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
Start a free Courses trial
to watch this video
The first part of our address book application that we'll address is creating a class to store our contacts. Unsurprisingly, we're calling this the Contact
class!
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 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
end
jason = Contact.new
jason.first_name = "Jason"
jason.last_name = "Seifer"
puts jason.full_name
nick = Contact.new
nick.first_name = "Nick"
nick.middle_name = "A"
nick.last_name = "Pettit"
puts nick.full_name
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
[MUSIC]
0:00
Hi, I'm Jason, and welcome to
Build an Address Book with Ruby.
0:04
We're going to be building a command
line program that will let someone
0:08
put in the names and
information of all of their contacts.
0:11
Then, we'll add in the ability to go
through, and search these contacts.
0:15
We're going to create our own classes for
0:20
each different part of
the address book program.
0:22
This will encompass the address book
itself, contacts, and their information.
0:25
What we're doing is
abstracting away the logic
0:30
of these different parts of
the program into their own classes.
0:33
This is a good example of
object oriented programming.
0:37
The process of figuring out what
logic goes to what class or
0:40
object is called domain modeling.
0:44
But that's not really super
important to remember.
0:46
Now, we have to start somewhere so
we'll go ahead and start with the user or
0:49
contact class.
0:53
Go ahead and launch a workspace, and
0:55
we'll begin to create the class
which will hold our contact.
0:56
Okay, so the first class that
we're going to create for
1:00
our address book is the contact class.
1:03
The contact is going to encompass the idea
of one of the people in your address book.
1:06
So let's go ahead,
I've launched a new Ruby workspace here.
1:14
And go ahead and hit File, and New File,
and we'll name this contact.rb.
1:16
So here we are, and
now we can go ahead and create our class.
1:23
Now let's think about what
the contact is going to have.
1:32
The contact for our purposes will have
a first name, middle name and last name.
1:36
Now we're going to want to refer
to those a little bit later,
1:43
so for right now, let's go ahead and
create attribute writers for those.
1:46
And since we have the writers, let's go
ahead and create methods to read them.
2:00
So, the first_name method is going
to return the first_name variable.
2:07
The middle_name method is going to
refer to the middle_name variable,
2:12
and the last_name method is going
to refer to the last_name variable.
2:18
Now, at the bottom of this file, let's
go ahead and just create a new contact,
2:27
make sure everything is working okay,
and we'll say jason.first_name is Jason.
2:30
I don't have a middle name.
2:39
And my last name is Seifer.
2:42
And let's go ahead and
just print out that contact.
2:48
Now I'm going to click down into
the bottom console here, and
2:50
type ruby contact.rb.
2:53
Okay, we have a contact.
2:57
And let me just go ahead and put
the first_name, space, and a last_name.
3:01
And just make sure that
this is working correctly.
3:10
Okay, that looks good.
3:12
Now when we think about our address book,
3:13
we may want to refer to
a contact in different ways.
3:15
Maybe we want to print out their
full name, like we did right here.
3:21
So let's go ahead and
create a full_name method,
3:25
that will print out
the contact's full name.
3:29
And instead of actually printing it out,
let's just go ahead and
3:33
return a string of the full name.
3:37
Now if we were just returning this,
3:39
we could do first_name,
middle_name, and last_name.
3:43
Now watch what happens
if we print this out.
3:54
We'll just print Jason.full_name.
3:57
Now notice I don't have a middle initial.
4:01
So when we print this out,
there's a little space right here.
4:06
So let's go ahead and fix that.
4:10
And instead of doing this, let's go ahead
and assemble this full name in parts.
4:13
So we'll say the full_name
is equal to the first_name.
4:20
Now right here full_name is just a local
variable inside the full_name method.
4:25
It's okay to name variables the same thing
as methods when you are inside a method.
4:30
You're not gonna have any conflict.
4:36
So we've got full_name and
then we can say,
4:38
if the middle_name is nil.
4:43
If the middle name is not nil, what we'll
do is add a space to this full name.
4:51
And then add the middle_name method which
returns the variable to that full_name.
5:03
And now we can just add
the last_name onto it.
5:11
And then we just return this variable.
5:20
And we do an implicit return as
the last line of the method.
5:23
Then when we run this,
we can see that works correctly.
5:30
And let's go ahead and
try one with a middle_name.
5:36
All right.
5:56
And we run that again.
5:58
And this works correctly.
6:00
And that is a pretty good
start to our contact class.
6:02
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