FAQ: Data Structures - (Re)Introduction to Iteration

This community-built FAQ covers the “(Re)Introduction to Iteration” exercise from the lesson “Data Structures”.

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

Learn Ruby

FAQs on the exercise (Re)Introduction to Iteration

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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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!

Beginner question.
Could I get a clarification about Hashes and especially placeholder symbol |x, y| in the below example?
Especially this line,
family.each { |x, y| puts “#{x}: #{y}”

I’m very confused about how the placeholder section |x, y| is laid out. I can understand what it means but it seems kind of “wrong”, therefore I think my mental model of it must be wrong too.

I’ve understood the key-value pain to be very, very similar to an index number and a value that number would refer to.
eg. say,
variable = “igloo”, var[1] index location would refer to value “g” (as index starts a 0).

Well I’m extremely confused about why in the placeholder text you’d refer to |x, y|. That’s almost as if you be using |the location of a value, and the value itself|.
Was this just an improvised answer because both the value and location was useful, eg Homer, and Dad, or is the |x, y| format to refer to |hash location, value| something we’ll use again and again. Thanks.
Sorry if I was confusing. I’m an extreme newbie with any coding language and only less than a week into my Ruby study.

friends = [“Milhouse”, “Ralph”, “Nelson”, “Otto”]

family = { “Homer” => “dad”,
“Marge” => “mom”,
“Lisa” => “sister”,
“Maggie” => “sister”,
“Abe” => “grandpa”,
“Santa’s Little Helper” => “dog”

friends.each { |x| puts “#{x}” }
family.each { |x, y| puts “#{x}: #{y}” }

1 Like

I also have the same question about "{ |x| puts “#{x}” }
I saw it in the previous exercise and that part was not explained at all.

When we iterate over an array or hash with .each the index isn’t used. .each just returns each element in an array or each key => value pair in a hash.

friends.each {|x| puts "#{x}"} #iterating over the friends array
#.each returns each element 1 at a time. x is the temporary placeholder for 
#the element. Whatever code is included in the code block can access the element
#referred to by x
family.each { |x, y| puts "#{x}: #{y}" } #iterating over the family hash
#x is the placeholder for the key. y is the placeholder for the value

Hope this helps.


Yes, it does. Thank you!

1 Like