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
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 upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
OK, so with our address class all set up,
0:00
we can now make our contacts
have many addresses.
0:03
So just like before, we're going to
need to require the address class.
0:07
And now we can add an attribute reader for
addresses as well.
0:12
And then, once again,
0:21
we need to set this to an empty array
when we initialize the contact class.
0:23
So we have our add phone number method.
0:31
Our add address method is
going to be very similar.
0:34
We're gonna take the attributes
of the address and
0:39
do kind street_1, street_2, city, state,
0:44
postal code, and
then we will initialize a new instance
0:49
of the address class like we
did with phone number above.
0:54
And now we can set all of the attributes
to the arguments to this method.
1:05
And then just like before,
1:28
we want to append this to
the internal array of addresses.
1:30
And since addresses is
an attribute reader,
1:40
we don't have to use the at sign to
address it as an instance variable.
1:43
And so now the last thing that we will do
here is we can print out phone numbers.
1:52
Let's go ahead and
print addresses as well.
1:56
And this will print out
the contact addresses.
2:05
We'll say addresses.
2:10
Again this is referencing
the instance variable,
2:11
which we have an attribute reader for.
2:14
And for each one of those,
we'll print out the address
2:17
as a string, and
we'll use the short version.
2:23
So once we have that.
2:31
We'll use our test and add an address.
2:35
Home, 123 Main Street.
2:41
There's no line two of that street.
2:43
And that's in Portland.
2:48
With that zip code.
2:51
So, we've got the add_address method
being called, sending an address and
2:53
let's go ahead and
print out the addresses as well.
2:57
Now let's go ahead and run this
contact file and see what happens.
3:04
Okay.
3:10
Here's Jason Seifer.
3:11
Here's the phone numbers and
here's the addresses.
3:12
But what is all this?
3:15
This is what was in the address file.
3:18
This code is getting run
3:21
when we require it up here on
line two of our contact file.
3:24
So now that we know this works,
we can get this out of here and save that.
3:29
Then clear my screen and run this again
and this works just like we expect it to.
3:37
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