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

Paul Jackson
Paul Jackson
7,585 Points

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!