Ruby - Hash Hash Indexing


To access a value, place its key between square brackets:

h1 = {'room1'=>'A', 
      'loc1' =>'C', 
      'loc2' =>'D' } 
puts(h1['room2'])            #=> 'The Throne Room' 

If you specify a key that does not exist, the default value is returned.

Recall that you have not specified a default value for h1, but you have for h2:

h1 = 
h2 ="Some value") 

p(h1['unknown_room'])      #=> nil 
p(h2['unknown_Product'])   #=> 'Some value' 

Use the default method to get the default value and the default = method to set it:

h1.default = 'A mysterious place'