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 Ruby Objects and Classes!
You have completed Ruby Objects and Classes!
Preview
Methods are extremely useful and powerful when writing classes. A method can manipulate the data inside of an instance of a class and return or format that data in new and interesting ways.
Code Samples
class Name
attr_accessor :title, :first_name, :middle_name, :last_name
def initialize(title, first_name, middle_name, last_name)
@title = title
@first_name = first_name
@middle_name = middle_name
@last_name = last_name
end
def full_name
@first_name + " " + @middle_name + " " + @last_name
end
def full_name_with_title
@title + " " + full_name()
end
end
name = Name.new("Mr.", "Jason", "", "Seifer")
puts name.full_name_with_title
nick = Name.new("Mr.", "Nick", "", "Pettit")
puts nick.full_name_with_title
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
Classes are useful for more than just
storing attributes for
0:00
the abstraction that we're working with.
0:03
We can write methods inside of our
classes.
0:06
Writing methods inside of classes is
powerful, because the methods can
0:10
apply only to the individual instance that
we're working with.
0:14
Let's go ahead and take our Name class and
add a method to it now using Workspaces.
0:19
So, we've got our Name class and I just
changed this to be an attr_accessor for
0:25
a title, first name, middle and last name.
0:30
So, we know that we're going to have all
of those different methods set for us.
0:32
Now, we can create our own methods to work
with in classes.
0:37
So, let's go ahead and create a method
called full_name.
0:42
[SOUND] And what this is going to do,
0:47
is return the first_name,
0:54
middle_name and last_name.
0:59
And we'll put a space in between there
too.
1:04
[BLANK_AUDIO]
1:06
Now instead of all of this different stuff
right here we can call name.full_name.
1:16
Let's go ahead and run that and see what
happens.
1:23
Okay.
1:28
This prints out.
1:29
Pretty much what we wanted.
1:30
But let's put a space here.
1:32
Okay.
1:33
[BLANK_AUDIO]
1:34
That looks good.
1:38
There we go.
1:41
The correct thing has been printed out.
1:42
Now we can also define another method.
1:46
We'll say full_name_with_title.
1:49
[BLANK_AUDIO]
1:50
And in this one we will return the title
and the space and
1:56
we can actually just call this method
because it returns a string.
2:01
This uses something called an implicit
return.
2:08
Which means that the last item on the line
is going to be the return value for
2:11
the method.
2:16
In this case, it's a string.
2:17
So we can use the results of that string
later.
2:20
And it would be just like calling the
method.
2:25
So now, we could write full_name_with_title.
2:28
And if we run this again, we get exactly
what we were expecting.
2:35
Now the nice thing about using classes and
2:40
objects [NOISE] is that the variables only
2:46
apply to that specific instance.
2:52
So if we created, let's say we call this
one nick,
2:57
this variable would only have access to
its internal variables.
3:04
They are local to this variable.
3:13
So if we run that again, we can see that
that gets printed out just like we expect,
3:17
and this allows us to abstract that logic
away.
3:22
And these are both fully contained and
have access only to their respective data.
3:25
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