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

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