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 Build an Address Book in Ruby Search Appending Contacts

Jared Armes
PLUS
Jared Armes
Courses Plus Student 6,302 Points

Appending to an array inside of a variable

Perhaps I am just misreading the challenge title, but they want me to append "contact" to the "contacts" array within the "address_book" variable. How is this possible, when they never asked me to define an instance of the "contacts" array? I tried this a couple of different ways but couldn't seem to figure it out. Any assistance at all would be appreciated; like I said, I feel that I may just be incorrectly interpreting the challenge.

address_book.rb
contact = Contact.new
contact.first_name = "My"
contact.last_name = "Name"

address_book = AddressBook.new
contacts = []

contacts.push(contact)

1 Answer

Tim Knight
Tim Knight
28,888 Points

Jared, the contacts array would be within the address_book object.

address_book = AddressBook.new
address_book.contacts << contact

You could also do using push below, but I prefer using << as a shorthand was to append.

address_book = AddressBook.new
address_book.contacts.push(contact)
Jared Armes
Jared Armes
Courses Plus Student 6,302 Points

Thank you, I like the shorthand version as well!