Ruby Hashes

Ruby Hashes

Ruby Hash is a collection of key-value pairs. The Ruby Hash is created using key-value pair within {}.

Hashes syntax

myMethod = Hash.new( "myHashName" )

Hashes example

myMethod = Hash.new( "myHashName" )
myHashName = {"10" => "A", "20" => "B", "30" => "C"}

puts "#{myHashName.keys}" 

Output

[“10”, “20”, “30”]

Hashes Elements example

myHash = { "a" => 10, "b" => 20 }

puts myHash['a']
puts myHash['b']

Output

10

20