Almost there! Finally, we’ll need to iterate over the array to print out each key-value pair to the console.

fruit = { "apple" => 2, "banana" => 3, "cherry" => 5 } fruit.each do |name, count| puts name + " " + count.to_s end
  1. In the example above, we create a hash called fruit that maps names of fruit to the amount that we own.
  2. Then, we iterate over .each key/value pair, storing the key as name and the value as count.
  3. Finally, we print out the key and value separated by a space. Note that we must first convert the value from a number to a string using .to_s before we can concatenate it.



Iterate over .each key/value pair in the frequencies hash.

Then, puts each word, a single space, and its frequency to the console, like in the example above.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Already have an account?