Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Build a Simple Contact List Hash and Array Iteration

Manish Giri
Manish Giri
16,266 Points

Not passing checks for printing value of hash keys

The challenge asks -

Using the each method, iterate over the contact_list array. Assign each array item to the local variable contact in the block and print out the value of the name and phone_number keys.

My code is attached. I also tried another way, both works in Repl.it - https://repl.it/EXBV/0

Not sure what's wrong here.

contact_list.rb
contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]
contact_list.each { |contact| contact.each {|k,v| print v}}

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, so the challenge is asking you to print only name and phone number values. Your code is more universal and would print all the values that might find their way into the hash (for example, if you add "age" => 21, this would be printed as well). You need to explicitly print the selected key values, nothing more:

puts contact['name']
puts contact['phone_number']
Manish Giri
Manish Giri
16,266 Points

Ah, understood it now. Thank You!