# FAQ: Data Structures - Iterating Over a Hash

This community-built FAQ covers the “Iterating Over a Hash” exercise from the lesson “Data Structures”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Iterating Over a Hash

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, I tried this not very smart solution to iterate over the hash. though I thought the syntax was ok, actually nothing is written on the console. Could you explain me why?

lunch_order.each { |element| element.each { |x, y| puts y }}

Thanks!
C

You’ve just gone a step too far. There is no value assigned to `y`. Here is the code provided in the lesson:

``````lunch_order = {
"Ryan" => "wonton soup",
"Eric" => "hamburger",
"Jimmy" => "sandwich",
"Cole" => "taco"
}
``````

`|element|` is assigned each element of the `lunch_order` hash in turn, so the first value assigned is `["Ryan", "wonton soup"]`. Then when you use `element.each`, you take these values: `["Ryan", "wonton soup"]` one at a time in turn, so `x` gets assigned `"Ryan"` and `y` gets nothing. Then in the next iteration of this nested `.each`, `x` gets assigned `"wonton soup"` and y again gets nothing. Then control passes back to the first `.each`, and the whole process repeats with `["Eric", "hamburger"]`, etc.

5 Likes

1 Like

Hello I tried these lines and it got stuck on “working”

1. lunch_order.each {|x, y| puts y}
2. lunch_order do |x, y|
puts y
end

I do have the wanted results but nothing happens… I can’t continue

2 Likes

I stuck in same step. After some unsuccessful trials, below is worked:

lunch_order.each {|key, value| puts value }

Mine worked, but it won’t finish out the lesson. It also didn’t show the solution in there when I checked that. This works though.

#Supposedly not right in Code Academy, but it worked to make sentences. Each prints on own line.

lunch_order = {
“Ryan” => “wonton soup”,
“Eric” => “hamburger”,
“Jimmy” => “sandwich”,
“Cole” => “taco”
}
lunch_order.each do |person, sides|
puts “\n#{person} wants: #{sides} sides.”
end

The instructions specify: `Please puts out the value of each pair (just the value, not the key)`

So, don’t output sentences or newline characters or keys. Simply `puts sides`

1 Like