Ruby Build an Address Book in Ruby Search Appending Contacts

Jared Armes
Jared Armes
6,242 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
MOD
Tim Knight
Treehouse Moderator 28,849 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
6,242 Points

Thank you, I like the shorthand version as well!