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

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

Ruby Loops Contact List Exercise 3

contact.rb
contact_list = []

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

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

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?