Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Build an Address Book in Ruby!
You have completed Build an Address Book in Ruby!
Preview
With all of our supporting classes set up, we can define an `AddressBook` class to hold our program.
Code Samples
require "./contact"
class AddressBook
attr_reader :contacts
def initialize
@contacts = []
end
def print_contact_list
puts "Contact List"
contacts.each do |contact|
puts contact.to_s('last_first')
end
end
end
address_book = AddressBook.new
jason = Contact.new
jason.first_name = "Jason"
jason.last_name = "Seifer"
jason.add_phone_number("Home", "123-456-7890")
jason.add_phone_number("Work", "456-789-0123")
jason.add_address("Home", "123 Main St.", "", "Portland", "OR", "12345")
nick = Contact.new
nick.first_name = "Nick"
nick.last_name = "Pettit"
nick.add_phone_number("Home", "222-222-2222")
nick.add_address("Home", "222 Two Lane", "", "Portland", "OR", "12345")
address_book.contacts.push(jason)
address_book.contacts.push(nick)
address_book.print_contact_list
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, so we've got our contact, phone
number, and address classes all set up.
0:00
Now our contact when we
initialize it has addresses and
0:05
phone numbers, but remember we're
building an address book here.
0:09
So an address book is going
to have many contacts.
0:14
Now let's go ahead and represent
an address book with another class.
0:18
So once again, do New File, and
we'll call this address_book.rb.
0:23
And this will be an address book class.
0:34
Now just like we did with our contacts,
having addresses and
0:38
phone numbers,
when we initialize an address book class,
0:42
we'll have our contact be an empty array.
0:50
And that's gonna be an instance variable.
0:53
So any method in address book
is going to have access to it.
0:56
Now, so that we don't have
to use the at sign each time
1:02
we want to refer to the contact,
1:06
we'll say we have an attribute reader for
contacts.
1:09
And let's go ahead and
create a method to print our contact list,
1:14
and then we can just iterate
through the contacts.
1:20
And we'll print out our contact, and we
can use the last name, first name format.
1:32
So now that we have
the address book class set up,
1:42
we can instantiate one, and then let's
go ahead and add a contact to it.
1:48
And actually I think I can just
grab this from our contact file.
1:55
I'm gonna copy that, And paste it,
and then I can take it out of here,
2:02
So that it's not cluttering up
the output like we had before.
2:13
So now we can say address_book.contacts,
and
2:20
we will add this contact
that we just created.
2:24
And now we can print
the contact list as well.
2:33
And let's just say Contact List.
2:39
Okay, now let's run address_book.rb,
and this is not gonna work correctly.
2:44
Can you guess why?
2:52
And here we get the message
uninitialized constant Contact.
2:54
And the reason is we haven't told Ruby
that we're gonna be using that class.
3:00
So we have to do it the same way that we
did in the contact class where we require
3:06
phone numbers and addresses.
3:10
We have to tell our address book,
To require contacts.
3:12
So let me clear my screen here and
print this again.
3:21
And hey, here we go.
3:25
Our contact list now has my name on it.
3:27
And go ahead and just add another contact.
3:33
Just to make sure it all prints correctly.
3:42
Okay.
And one more time.
4:18
All right.
4:21
So far,
our contact list is looking pretty good.
4:22
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up