FAQ: Data Structures - Iterating Over Hashes

This community-built FAQ covers the “Iterating Over Hashes” 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 Iterating Over Hashes

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!

So with this lesson having Super Heroes in it. I wrote my code as

secret_identities.each do |super, human|
puts “#{super}: #{human}”
end

Only to learn super means something totally different to Ruby so it broke. Changed super to codename like so and it worked perfectly!

secret_identities.each do |codename, human|
puts “#{codename}: #{human}”
end

Hopefully no one else tries super even though coding is a super power :slight_smile:

3 Likes

I did the same using:

secret_identities.each do |alias, real|

… and it didn’t like that.

Don’t use “alias” either, folks.

6 Likes

I did the same, but alias, identity.

The lesson starts by talking about place holder names for key value pairs, so I presumed making my own would work.
Unfortunately, it wants you to place “key” and “value” in key and value.
:man_facepalming:

yep, i used “alias” and they didn’t like that… better instructions needed

I tried this out 2 ways, both work;

secret_identities.each { |keyvalue| 
  puts keyvalue[0] + ": " + keyvalue[1]
}

secret_identities.each { |key, value| 
  puts key + ": " + value
}

I’d really like to know more about how “each” can do this, in the first I return an array of two elements in the second split the array and return 2 variables.

Thank you! You totally saved me :slight_smile:

1 Like