A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.
grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
----------------------------------------------------------------------
restaurant_menu = { "noodles" => 4, "soup" => 3, "salad" => 2 }
restaurant_menu.each do |item, price|
puts "#{item}: #{price}"
end
this will print:
noodles: 4
soup: 3
salad: 2
----------------------------------------------------------------------------
prices = {
"apple" => 0.52,
"banana" => 0.23,
"kiwi" => 1.42
}
----------------------------------------------------------------------------
sounds = Hash.new
sounds["dog"] = "woof"
sounds["cat"] = "meow"
grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
----------------------------------------------------------------------
restaurant_menu = { "noodles" => 4, "soup" => 3, "salad" => 2 }
restaurant_menu.each do |item, price|
puts "#{item}: #{price}"
end
this will print:
noodles: 4
soup: 3
salad: 2
----------------------------------------------------------------------------
prices = {
"apple" => 0.52,
"banana" => 0.23,
"kiwi" => 1.42
}
----------------------------------------------------------------------------
sounds = Hash.new
sounds["dog"] = "woof"
sounds["cat"] = "meow"