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 Assigning Hash Values From Methods

Error encountered after appending hash to array. Code Challenge 3 of 3

Hello,

I'm working on Stage 3 of "Build A Simple Contact List", and in Code Challenge 3 of 3, you are asked to "Append the contact hash to the contact_list array." I enter the code that - to my understanding - should work:

contact_list.push(contact)

Error message: "Bummer! The contact_list array did not contain the contact hash."

Entire code listed below:

contact_list = []

contact = {"name" => get_name, "phone_number" => get_phone_number } def get_name return "" end

def get_phone_number return "" end contact_list.push(contact)

Thanks, in advance, Rachman

contact.rb
contact_list = []

contact = {"name" => "", "phone_number" => "" }

2 Answers

Iris Avalon
Iris Avalon
14,477 Points

I believe you don't need to add the get_name and get_phone_number methods yourself.

All you really need to do is:

contact_list = []

contact = {"name" => get_name, "phone_number" => get_phone_number }

contact_list << contact

This should work just fine. I'm guessing the test assumes the get_name and get_phone_number methods are already defined elsewhere, and is probably looking to see if what they returned is present in the hash.

Hope this helps!

Hi Raven, Thanks for replying; I tried contact_list << contact contact_list.push(contact) and I'm still getting the error message. I've run out of options to try, at least for now.

Iris Avalon
Iris Avalon
14,477 Points

Oh you don't have to add contact_list.push(contact)The contact_list << contact is just a shorthand way of writing the same thing.

I tried Raven's solution of contact_list << contact and it worked like a charm. Thanks!