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

How do I append? Where did we learn that method?

Ruby Loops Contact List Exercise 3

contact_list = []

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

def get_name
  item = gets.chomp
  return item
def get_phone_number
  item = gets.chomp
  return item

contact_list << contact

1 Answer

Hello Francisco,

So it looks like you're already attempting to append with that last line:

contact_list << contact

Where contact_list is your empty array and contact is a hash object.

The << symbol which is also referred to as push, shovel or append; pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together. - Ruby Docs

To give you another example:

foo_array = ["bar"]

foo_array << "Francisco"

print foo_array #=> ["bar", "Francisco"]

I haven't yet worked through the Ruby track in Treehouse so I can't answer your last question. However I can recommend you take a peak at the Ruby docs. I found the syntax for appending by using the browsers search tool (cmmd + f on mac) and typing append.

I hope this is helpful!

Cheers, Jacob

Hey Jacob, Thanks for the info! Still, I am unable to complete the challenge. Maybe I should use Array.new?