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

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