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

contactlist Ruby Challenge Task 1

I am missing something.

contact_list.rb
contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]
contact_list.each do |contact|
  puts "Name:  #{contact["name"]}"

 contact["phone_numbers"].each do |phone_number|
   puts "Phone:  #{phone_number}"
  end
end

2 Answers

Stuart Craig
Stuart Craig
11,914 Points

Firstly, you don't have a key called "phone_numbers" in any of the hashes in your contacts list, so it can't find it. Also you don't need the second each method, since it can't be called on the "phone_number" key anyway. Try this.

contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]
contact_list.each do |contact|
  puts "Name:  #{contact["name"]}"
  puts "Phone:  #{contact["phone_number"]}"
end

I hope this helps. I haven't actually checked out the exercise itself, but this works.

Hi Brenda,

Great efforts and still very close. You don't have to be over thinking. :)

I've remove some in your code, here's example.

contact_list.each do |contact|

  puts contact["name"]
  puts contact["phone_number"]

end

-Salman