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
Ruby gives us shortcuts to write common pieces of code. The process of accessing and writing attributes in a class can be done using `attr_writer` and `attr_accessor` to ease repeating code.
This video doesn't have any notes.
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
In addition to the attr_reader method,
0:00
Ruby gives us some other shortcut methods
for reading and writing attributes.
0:03
Let's take a look at how those work now,
using workspaces.
0:08
Now Ruby gives us some other methods like
attr_reader, that we can use in our code.
0:13
And let's go ahead and see how this works
right now.
0:19
If I open up IRB, I can load in this file
to work with,
0:22
by typing the word load and the name of
the file.
0:28
And what that will do, will be to load
this class into our IRB session.
0:32
As if we had written it inside of IRB.
0:39
So type load, and then a space, a quote,
and
0:42
then the path to the file, which is this
directory,
0:47
which is the dot, and a slash, the name of
the file, which is name.rb.
0:53
Now you will notice that this gets
printed, but we don't quite have access
0:58
to the instance variable name just yet, so
let's go ahead and create one.
1:05
We will say name equals Name.new,
"Mr.", "Jason", "", "Seifer".
1:10
Okay.
1:21
Now we have this little name object that
we're working with.
1:22
Since we put the attr_readers in here
that's going to
1:25
allow us to read these different
attributes.
1:28
So we could say name.title and it would
give us back the title.
1:30
But we can't change that.
1:35
For example, if I somehow became a doctor,
Ruby tells us there's an undefined
1:39
method `title=' for this particular Name object.
1:47
So, how would we do that?
1:54
Let's go ahead and exit IRB here.
1:56
And let's go back to this particular file.
2:00
If I wanted to be able to reset the title
later,
2:05
I would have to define a method to do it.
2:08
[SOUND] And we do that by calling define,
and
2:11
then the name of the method, which is
going
2:16
to be the same as the instance variable
name.
2:21
title, and then the equals sign.
2:27
[SOUND] And then we pass in a new title.
2:30
So now we type instance variable title
equals.
2:35
This new title that's being called on the
method.
2:42
So I'm gonna do a scroll down here.
2:45
And just print out the title, put
name.title.
2:49
And now we saw before when we were working
with this, we could not call name.title.
2:57
Let's try it again.
3:01
[SOUND] And we're just gonna print this
out twice,
3:02
and let's be a little bit more thorough
here.
3:09
[SOUND] And we can call that.
3:15
[SOUND] Now the same code that didn't work
before.
3:19
[SOUND].
3:25
Go ahead and clear my screen.
3:26
Now, if I run name.rb, we can see that the
title was changed.
3:28
So, let's look at this.
3:36
What we're doing is setting this title
instance variable to a new value which we
3:39
send in to this method.
3:44
Now this is a really common pattern,
again, in Ruby.
3:47
So, Ruby gives us another way to work with
that and
3:52
that is by defining a writer in addition
to a reader.
3:56
And then Ruby will automatically take care
of this for us.
4:05
[SOUND] So if I run this again, the same
thing
4:08
is printed out, and Ruby is actually so
nice.
4:15
That it wraps both of these patterns into
one different method call,
4:22
which is called attr_accessor.
4:28
[NOISE] So I could now take out that
[NOISE] and
4:31
remove the title from the list of readers.
4:35
[SOUND] And if I run this again the same
thing should print out.
4:40
Now you might be wondering why you would
ever use attr_reader and
4:45
attr_writer instead of just using
attr_accessor.
4:49
The difference is thinking about it in
terms of readability of your code.
4:53
You may only ever be reading certain
attributes, like a first name,
5:00
middle name and last name, and only
writing certain attributes.
5:05
Be as explicit as possible.
5:09
If you know you're only going to be
reading certain attributes,
5:12
only set those as a reader.
5:15
And the same thing with writing
attributes.
5:17
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