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 trialJoseph Ruben
2,752 Points"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_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
23,947 PointsYou 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)
Gilbert Kennen
10,661 PointsI 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
25,256 PointsThis'll work too:
contact_list = []
contact = {"name" => get_name, "phone_number" => get_phone_number }
contact_list.push(contact)
Kyle Shamblin
9,945 PointsKyle Shamblin
9,945 PointsThis! ^^^