Ruby - Hash Hash Indexing

Introduction

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

h1 = {'room1'=>'A', 
      'room2'=>'B', 
      '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 = Hash.new 
h2 = Hash.new("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:

p(h1.default) 
h1.default = 'A mysterious place'