Ruby On Rails Classroom image

Neha  Jaggi / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  |1 | 2|Last
Lessons:- Hashes and Symbols

Hashes and Symbols

Hashes are essentially a generalization of arrays: You can think of hashes basically like arrays, but not limited to integer indices. (In fact, some languages, especially Perl, sometimes call hashes associative arrays for this reason.) Instead, hash indices, or keys, can be almost any object. For example, we can use strings as keys:

>>  user  =   {}                                                                      # {} is an empty hash.
=> {}
>> user   ["first name"] =  "Michael"                                     # Key "first name", value "Michael"
=> "Michael"
>> user   ["last name"] =  "Hartl"                                         # Key "last name", value "Hartl"
=> "Hartl"
>> user   ["first name"]                                                       # Element access is like arrays.
=> "Michael"
>> user                                                                            # A literal representation of the hash 
=> {"last name"=>"Hartl", "first name"=>"Michael"}

 

Hashes are indicated with curly braces containing key-value pairs; a pair of braces with no key-value pairs—i.e., {}—is an empty hash. It’s important to note that the curly braces for hashes have nothing to do with the curly braces for blocks. (Yes, this can be confusing.) Although hashes resemble arrays, one important difference is that hashes don’t generally guarantee keeping their elements in a particular order.8 If order matters, use an array.  

Instead of defining hashes one item at a time using square brackets, it’s easy to use a literal representation with keys and values separated by =>, called a ‘‘hashrocket’’:

>> user = { "first name" =>  "Michael",   "last name"  =>  "Hartl" }
=> {"last name"=>"Hartl", "first name"=>"Michael"}

   Here I’ve used the usual Ruby convention of putting an extra space at the two ends of the hash—a convention ignored by the console output. (Don’t ask me why the spaces are conventional; probably some early influential Ruby programmer liked the look of the extra spaces, and the convention stuck.)

        So far we’ve used strings as hash keys, but in Rails it is much more common to use symbols instead. Symbols look kind of like strings, but prefixed with a colon instead of surrounded by quotes. For example, :name is a symbol. You can think of symbols as basically strings without all the extra baggage:9

>>  "name"  .split  ('')
=> ["n",  "a",  "m",  "e"]
>>  :name .split('')
NoMethodError:  undefined  method  `split'  for  :name: Symbol
>>  "foobar  .reverse
=>  "raboof"
>>   :foobar.  reverse
NoMethodError:  undefined  method  `reverse'  for  :foobar: Symbol
 

 Symbols are a special Ruby data type shared with very few other languages, so they may seem weird at first, but Rails uses them a lot, so you’ll get used to them fast.
               In terms of symbols as hash keys, we can define a user hash as follows:

>>  user = { :name =>   "Michael Hartl",   :email =>   "michael@example.com" }
=>  { :name=>  "Michael Hartl",   :email=>   "michael@example.com"}
>>  user     [:name]                                # Access the value corresponding to :name.
=>  "Michael Hartl"
>>  user  [:password]                              # Access the value of an undefined key.
=>  nil

We see here from the last example that the hash value for an undefined key is simply nil. 

          Since it’s so common for hashes to use symbols as keys, Ruby 1.9 supports a new syntax just for this special case:

>>  h1 = {  :name =>   "Michael Hartl",  =:email=>   "michael@example.com" }
=>  { :name=>  "Michael Hartl",  :email=>  "michael@example.com"}
>>  h2 = {  name:  "Michael Hartl",   email:     "michael@example.com" }
=>  {:name=>"Michael Hartl", :email=>"michael@example.com"}
>>  h1  ==  h2
=>  true

 

 
 
 
image
Neha  Jaggi

Skills    Ruby On Rails

Qualifications :- High School - , College/University - Graphic Era Hill University, Dehradun, College/University - ,
Location :-Dehradun,Dehradun,UTTARAKHAND,India
Description:-

Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Skilled in Web Technologies (Ruby on Rails, PostgreSQL, php, Laravel and AJAX). 


Explore
 

  Students (0)