Ruby - Hash Creation Literal

Introduction

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:

Demo

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

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

Result

Related Topic