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

"Append the contact hash to the contact_list array." why will my code not append

"Append the contact hash to the contact_list array."

why will my code not append

contact.rb
contact_list = []

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

contact_list.push(contact["name"] = get_name())
contact_list.push(contact["phone_number"] = get_phone_number())

contact_list << contact

3 Answers

Noah Yasskin
Noah Yasskin
23,947 Points

You may be over thinking it. I think this is the answer you want:

contact_list = []

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

contact_list.push(contact)

I don't know what you are attempting to do here, but are you intending to push get_name() and get_phone_number() onto your contact_list before pushing the contact? You call .push twice and then you use the << push operator at the end.

I suspect you intend to do this:

contact_list = []

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

contact["name"] = get_name()
contact["phone_number"] = get_phone_number()

contact_list << contact

The code as you have it results in contact_list having three elements at the end. The result of get_name(), the result of get_phone_number(), and the contact.

Adam Vandover
Adam Vandover
25,256 Points

This'll work too:

contact_list = []

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

contact_list.push(contact)