Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
acp2
Code Samples
Contact Class:
Add to require address class:
require './address'
Our initialize method now looks like this:
class Contact
attr_writer :first_name, :middle_name, :last_name
attr_reader :phone_numbers, :addresses
def initialize
@phone_numbers = []
@addresses = []
end
Here's our add_address method:
def add_address(kind, street_1, street_2, city, state, postal_code)
address = Address.new
address.kind = kind
address.street_1 = street_1
address.street_2 = street_2
address.city = city
address.state = state
address.postal_code = postal_code
addresses.push(address)
end
And we can print them as well:
def print_addresses
puts "Addresses"
addresses.each { |address| puts address.to_s('short') }
end
Then call it as follows:
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")
puts jason.to_s('full_name')
jason.print_phone_numbers
jason.print_addresses
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
teamz
44,723 Points1 Answer
-
Ulfar Ellenarson
5,277 Points3 Answers
-
Erik Nuber
20,629 Points0 Answers
-
Tina Maddox
28,102 PointsOnce address was added contact names do not print. No errors.
Posted by Tina MaddoxTina Maddox
28,102 Points2 Answers
-
Mark Simcoe
1,046 PointsWhat is the reason for using "!@middle_name.nil?" instead of "@middle_name"?
Posted by Mark SimcoeMark Simcoe
1,046 Points1 Answer
-
Kevin Naegele
10,868 Points2 Answers
-
Benjamin Raskin
4,075 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
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