Learn
Data Structures
Iterating Over Hashes

When iterating over hashes, we need two placeholder variables to represent each key/value pair.

restaurant_menu = { "noodles" => 4, "soup" => 3, "salad" => 2 } restaurant_menu.each do |item, price| puts "#{item}: #{price}" end
  1. In the example above, we create a new hash called restaurant_menu.
  2. Then, we loop through the restaurant_menu hash and assign the key to item and the value to price for each iteration.
  3. Finally, we puts out:
noodles: 4 soup: 3 salad: 2

Instructions

1.

Use .each to iterate over the secret_identities hash.

Use puts to print each key-value pair, separated by a colon and a space (:), to the console, just like the example above.

Folder Icon

Take this course for free

Already have an account?