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