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

philip bae
philip bae
2,556 Points

I feel like the task doesn't explicitly state what they mean by a 'contact' local variable in the block.

Anyway, my code is below, can someone please tell me the issues i must fix?

Thanks

contact_list.rb
contact_list = [
  {"name" => "Jason", "phone_number" => "123"},
  {"name" => "Nick", "phone_number" => "456"}
]

contact_list.each do |contacts|
  contacts.push(contacts)
end

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

1 Answer

Mike Hickman
Mike Hickman
19,817 Points

Hi Philip,

In this case, it means to use |contact| when you are doing contact_list.each. It's basically saying "for EACH contact, name it 'contact'". So, at that point, you can use the below to pull the values from each contact:

contact['name']
contact['phone_number']

You have the right idea, but you're doing more than needed. All you need is the .each block, and not the tons of code below it. To get you started:

contact_list.each do |contact|
  puts 'Contact Name: ' + contact['name']
  # now 'puts' your phone_number on this line
end

The "local variable" is whatever you choose to name it within the |contact| pipes after 'do'

If you want to keep using string interpolation like you are currently doing with #{}, you can:

puts "Contact Name: #{contact['name']}"

Good luck!

Mike