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
Now our contact when we
initialize it has addresses and
0:00
phone numbers, but remember we're
building an address book here.
0:00
So an address book is going
to have many contacts.
0:05
Now let's go ahead and represent
an address book with another class.
0:09
So once again, do New File, and
we'll call this address_book.rb.
0:14
And this will be an address book class.
0:25
Now just like we did with our contacts,
having addresses and
0:29
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