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

Garret Saarinen
Garret Saarinen
3,942 Points

Is there a better way that this can be explained? I don't know exactly what this task is asking.

The instructions are- "Append the contact variable to the contacts array inside of the address_book variable."

but everything I write seems to only result in "Task 1 is now failing" so I'm stuck. I don't see a contacts array so I assume I'm supposed to create one to but it isn't very explicate. I initially tried

address_book << contacts

or

address_book.push(contacts)

but that doesn't seem it.

address_book.rb
contact = Contact.new
contact.first_name = "My"
contact.last_name = "Name"
contacts = Array.new(contact)

address_book = AddressBook.new

1 Answer

Hi Garret,

I agree that the question is confusing. If you launch the workspace on video before the challenge it gives some important context as to what you have to work with. The AddressBook class defines a getter for contacts through the attr_reader and it also creates a contacts array in the initialize method like below:

class AddressBook
  attr_reader :contacts

  def initialize
    @contacts = []
  end
end

The attr_reader helper really creates something that looks like this

def contacts
  @contacts
end

This means you access it as a method. So for the challenge, you would need to write something like what's below. You append contact to the instance variable contacts.

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

address_book = AddressBook.new
address_book.contacts << contact

Hope this helps ?

Garret Saarinen
Garret Saarinen
3,942 Points

Ahhhh that makes total sense now. I had some time lapsed before completing this task so I forgot about what was actually in the workspace.

Thanks so much Paul!