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
p2
Code Samples
class Contact
attr_writer :first_name, :middle_name, :last_name
def first_name
@first_name
end
def middle_name
@middle_name
end
def last_name
@last_name
end
def first_last
first_name + " " + last_name
end
def last_first
last_first = last_name
last_first += ", "
last_first += first_name
if !@middle_name.nil?
last_first += " "
last_first += middle_name.slice(0, 1)
last_first += "."
end
last_first
end
def full_name
full_name = first_name
if !@middle_name.nil?
full_name += " "
full_name += middle_name
end
full_name += ' '
full_name += last_name
full_name
end
def to_s(format = 'full_name')
case format
when 'full_name'
full_name
when 'last_first'
last_first
when 'first'
first_name
when 'last'
last_name
else
first_last
end
end
end
jason = Contact.new
jason.first_name = "Jason"
jason.last_name = "Seifer"
puts jason.to_s
puts jason.to_s('full_name')
puts jason.to_s('last_first')
nick = Contact.new
nick.first_name = "Nick"
nick.middle_name = "A"
nick.last_name = "Pettit"
puts nick.to_s('first_last')
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 have our contact class here, and
0:00
we've got this full name method
which returns the full name.
0:03
We might want to refer to somebody
by their last name first, and
0:07
then their first name.
0:11
So let's go ahead and
create that method as well.
0:12
We'll call that last_first.
0:19
And we can do that same
thing that we did last time.
0:23
And we can say last_name, or last_first,
starts with the last_name this time.
0:25
And then normally we will add a comma,
0:32
and then we'll add the first name.
0:38
And now we can do the same check for
the middle name.
0:43
We'll see if that's nil.
0:50
And then, if it is not nil,
We will add a space,
0:54
And then we'll take just the first
letter of the middle name.
1:05
And we will add a dot at the end of that.
1:14
And then finally we can return
the last name, first name.
1:19
So I'm gonna save that, and
let's just make sure that works.
1:22
So, we've got our two
contacts in this file.
1:36
Now, let's just run it and
make sure it works okay.
1:39
Okay, that is returning
exactly what we want to.
1:43
So now that we've got two different
ways of displaying these methods,
1:48
let's go ahead and override the
to_s method, so that we can call that.
1:52
So here we go, we called to_s.
2:02
And for right now, we'll say the format
by default is going to be full_name.
2:05
Now we can use a case statement
to see what the format is.
2:13
So, if the format is full_name,
2:20
we can just return the full name
by calling the full name method.
2:24
When it's last name, first name,
we can call the last, first method.
2:33
And then we can do the same thing with
the first name, And the last name.
2:40
And by default, let's just go ahead and
return first and
2:50
last, which is not a method
we have written yet.
2:55
So let's go ahead and write that.
3:00
That does not include the middle names.
3:04
So we can just say first_name,
plus space, plus last name.
3:06
Now, we can call jason.to_s, And
3:17
then we can call this with
either full name, Or last first.
3:23
And we'll try it again on Nick.
3:34
And we know that the others work so let's
just try calling first_last on this one.
3:39
Click down into the console,
clear my screen here, and
3:47
when we run that it appears
to be working correctly.
3:51
Now we don't necessarily need this
to_s method right now, but
3:55
this will come in handy
a little bit later,
3:58
when we're writing the different
parts of our address book.
4:01
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