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
When using variables inside classes, only instance variables, which are prefixed with the `@` character, will be visible to all of the methods in the class. A variable that only exists inside of a code block or method is called a local variable. The whole concept is called scope.
Code Samples
class Name
attr_accessor :title, :first_name, :middle_name, :last_name
attr_reader :first_and_middle_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_and_middle_name = @first_name + " " + @middle_name
@first_and_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
When we create different variables inside
of a class,
0:00
not every method can see the variables
that we use.
0:03
If we wanna make a variable visible to
different methods inside of a class,
0:08
we call it an instance variable.
0:12
That means that the variable should be
visible to
0:15
the whole instance of the class.
0:18
If we want a variable that's only visible
inside of the method or
0:21
block that we're working with, that's
called a local variable, and
0:25
this whole concept is called the scope of
a variable.
0:29
Let's see how that works now using
Workspaces.
0:34
So, here's our Name class, and
0:38
let's talk a little bit about local versus
instance variables.
0:40
So, here we are in the full_name method,
and we also have full_name_with_title.
0:46
Let's go ahead and
0:51
pretend that we were creating a couple
different variables inside of this method.
0:53
So, let's say that we wanted to assemble
the first and middle name.
0:59
We could create a variable that says first
_and_middle_name.
1:05
We generally wouldn't do something like
this, but just follow along for a second.
1:10
[BLANK_AUDIO]
1:14
We now have access to this first
_and_middle_name variable right here.
1:21
So, what we could do is, I'm gonna comment
this out, and
1:26
we could say first_and_middle_name plus
space plus last_name.
1:32
And that should still return the correct
item.
1:42
Now let's go ahead, get this out of here
for now.
1:46
So let's run this by typing ruby name.rb.
1:50
And it's printing the same thing, which is
what we wanted.
1:56
But we do not have access to this
first_and_middle_name variable
1:59
outside of this full_name method.
2:05
The only way that we would have access to
it
2:09
would be if we made it an instance
variable.
2:12
And we can see this if we use irb.
2:15
And we can do a shortcut to what I showed
you earlier where you type in load and
2:20
the path, and by typing irb -r name.rb.
2:25
Oops, looks like I forgot the path.
2:31
[BLANK_AUDIO]
2:33
Okay.
2:37
Now let me go ahead and create this name
variable.
2:39
[BLANK_AUDIO]
2:43
Now we can see printing out full_name
works when we call the method.
2:54
But if we were trying to call
first_and_middle_name,
2:59
this would not work.
3:02
[BLANK_AUDIO]
3:03
This doesn't work because it is just a
local variable.
3:06
[BLANK_AUDIO]
3:10
If we wanted to change it to an instance
variable, we would then have access to it.
3:13
Now, since we changed this file,
3:22
we're going to have to reload it into our
irb session.
3:24
Now let me go ahead and create a name
again.
3:30
[BLANK_AUDIO]
3:32
Now when we call first_and_middle_name,
3:44
we're getting this undefined method again
still.
3:47
And the reason is we haven't written an
attribute reader for it.
3:51
So let's go ahead and do that.
3:55
[BLANK_AUDIO]
3:56
And we'll reload this.
4:04
[BLANK_AUDIO]
4:05
And we can see that that's nil.
4:20
If we call full_name, it will have been
set when we called the full_name method.
4:23
And now we can see first_and_middle_name
is correct.
4:32
So, visibility of variables is something
very important to keep in mind,
4:37
and this is referred to as the scope of a
variable.
4:43
The scope, meaning where we are in the
method and what's visible variable-wise.
4:47
You need to sign up for Treehouse in order to download course files.
Sign up