Ruby - Hash Creation Literal


There is a shorthand way of creating Hashes and initializing them with key-value pairs.

Just add a key followed by => and its associated value.

Each key-value pair should be separated by a comma and the whole lot placed inside a pair of curly brackets:


h1 = {'room1'=>'A', 
      'loc1' =>'C', 
      'loc2' =>'D' } 

puts h1#  w ww.j av  a  2  s  . c  om


Related Topic