Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 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
Contacts are going to have both phone numbers and addresses. In this video, we're going to define the `to_s` method on the address class and have it be able to take a formatting argument.
Code Samples
Address.rb:
class Address
attr_accessor :kind, :street_1, :street_2, :city, :state, :postal_code
def to_s(format = 'short')
address = ''
case format
when 'long'
address += street_1 + "\n"
address += street_2 + "\n" if !street_2.nil?
address += "#{city}, #{state} #{postal_code}"
when 'short'
address += "#{kind}: "
address += street_1
if street_2
address += " " + street_2
end
address += ", #{city}, #{state}, #{postal_code}"
end
address
end
end
home = Address.new
home.kind = "Home"
home.street_1 = "123 Main St."
home.city = "Portland"
home.state = "OR"
home.postal_code = "12345"
puts home.to_s('short')
puts "\n"
puts home.to_s('long')
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 that our contact class is all set up,
0:00
we can start working on
the other parts of the program.
0:02
Since they're not quite
addressed by built in classes,
0:05
we're going to have to write our own.
0:07
Get it?
0:09
Addressed?
0:10
It's a bad pun about addresses.
0:11
Anyway, we'll be writing
our own address class now
0:13
in addition to another supporting
class for our program.
0:16
Let's get to it using workspaces.
0:19
So our contacts have phone numbers.
0:22
Now let's go ahead and
have addresses for our contacts as well.
0:24
Now we can do that by creating
another class for an address.
0:29
So we'll go ahead and create a new file,
you can do that by right clicking and
0:35
selecting new file on the side bar,
or go file and new file.
0:40
And we will call this address.rb.
0:44
So now we can create our address class.
0:50
And addresses are gonna be
similar to phone numbers.
0:53
They can have a street with potentially
two lines and then a city, state, and
0:56
postal code.
1:01
Now, we also wanna know
what kind of address it is.
1:02
So, we'll put that in there
as an attribute as well.
1:05
So we'll have the kind,
the first line of the street address,
1:11
second line of the street address,
the city, state, and postal code.
1:16
Now just like with phone numbers, let's
go ahead and override the to_s method.
1:23
Except there are two different ways that
we may wanna be formatting addresses.
1:29
So we can use that pattern that we
used earlier with contacts, and
1:34
we'll say the format that we want
by default is going to be short.
1:38
So now we can look at the format.
1:43
And let's go ahead and
create a little string here,
1:47
to hold our address,
which we will implicitly return.
1:52
Now when we're using the short format,
we will add on the kind of address,
2:00
so we will say home, colon and
then the street address all on one line.
2:10
That will be the first line
of the street address.
2:20
And then we can say,
2:23
if there is a second line.
2:27
We'll add a space to this string
because remember it's all on one line.
2:32
And then the street two attribute and
2:37
then we can return the comma and
2:42
the city, state and postal code.
2:47
And let's just go ahead and print that
out real quick, and make sure it works.
2:54
This is not my real home address.
3:17
And let's just go ahead and
print that out using the short format.
3:22
So now we'll run this very quickly and
make sure it works.
3:27
And type ruby address.rb.
3:30
Okay, that looks good, and let's go ahead
and create the long format as well.
3:33
So when the argument that we send
in to this to_s method is long,
3:39
we will say it is the street_1
variable plus a new line.
3:45
[NOISE] And then we can also say,
3:52
here is the street two variable.
3:56
Plus a new line.
4:00
And remember we can put if statements
all on the same line if we want to.
4:03
So we'll just say we'll add on to the
address variable the street two variable,
4:10
and a new line if street two is not nil,
and
4:18
then we could also add on the city,
state and postal_code.
4:22
Now let's just go ahead and
print a new line here.
4:35
And print it out in
the long format as well.
4:42
Okay, that looks good.
4:48
We've got our home address printed in
the short format and in the long format.
4:49
In our next video,
we'll add this back to the contact class.
4:56
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