Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,440 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!