Locked lesson.
About this lesson
What is an instance variable and how is it different from a regular variable?
Exercise files
Download this lesson’s related exercise files.
Instance Variables.docx60.4 KB Instance Variables - Solution.docx
60.7 KB
Quick reference
Instance Variables
Let's look at instance variables.
When to use
Instance variables are used inside your class and can be used in any method within your class.
Instructions
An instance variable starts with an @ sign. For instance:
@side_length
Hints & tips
- Instance variables start with an @ sign
- They can be used in all your class methods
- 00:04 In this video, I want to continue on talking about classes,
- 00:07 specifically we want to look at the instance variable right here.
- 00:11 So an instance variable always starts with the @ sign, that's what designates it,
- 00:15 that's what makes it an instance variable.
- 00:17 And our initialize method, I think pretty much all the time,
- 00:21 this is going to be the totality of all your initialized methods.
- 00:25 So they're just going to pass in whatever parameters you want to let into your class
- 00:29 and assign it.
- 00:30 Why?
- 00:31 Why do we do that?
- 00:32 Well, there's a concept called scope, and
- 00:34 in scope where you can use your variables, it matters.
- 00:38 So a regular variable, you can use it outside of wherever,
- 00:42 an instance variable, you can only use it inside of the class.
- 00:47 It doesn't work anywhere else, the scope of it is inside of its class.
- 00:52 Now, we can use this not just in this method,
- 00:55 we can use this in every method inside of our class.
- 00:58 Now, if we had some other variable and it was just a regular variable,
- 01:02 we couldn't use it in an another method, right?
- 01:04 But these instance variables, we can, so it's very important.
- 01:08 So let's create a new method called blueprint for now.
- 01:13 We can use our instance variable of side_length inside this method,
- 01:21 so we might say puts Your square as a side length of, and
- 01:27 let's interpolate this and go @side_length.
- 01:34 So if we save this guy, and let's come down here.
- 01:38 Now, lets say we want to call this method, we could just go puts my_square.blueprint.
- 01:46 Remember, earlier when we wanted a random number we called Random.rand,
- 01:50 same thing here, we're calling my_square.blueprint
- 01:54 Calling the blueprint method in our my_square class,
- 01:58 which is actually this square class, so if I save this and
- 02:03 run it you get, Your square has a side length of 10.
- 02:07 Ain't that cool?
- 02:08 So if we change this to 20, save it, run it again,
- 02:12 Your square has a side length of 20, I think that's really cool.
- 02:15 So puts my_square.inspect,
- 02:20 now, if we look at our class, nothing has changed,
- 02:22 we still have our side_length=20, this is where it exists in memory.
- 02:26 But now, we can use this @side_length anywhere we want inside of our class.
- 02:32 Now, like I've said in the past,
- 02:33 it's not a good idea to use puts inside of a method.
- 02:37 If we save this and run it again, same thing, we're just returning it.
- 02:41 Because we're puts-ing it out onto the screen.
- 02:44 One kind of neat thing, there's a internal Ruby method called to_s.
- 02:49 So if we change the name of this to to_s, and we don't have that return.
- 02:55 I don't know if we save this, and if we just puts out now our class itself,
- 03:02 we get the same, Your square has a side length of 20 because
- 03:07 the to_s replaces that gobbledygook that we had earlier.
- 03:12 We can do an inspect, and we still get the gobbledygook, but if we do it without
- 03:18 the inspect, save it and run it, we get the, Your square has a side length of 20.
- 03:24 That's sort of interesting trivia, we don't need to know that right now.
- 03:27 But anyway, those are instance variables, very important to class.
- 03:31 The scope of an instance variable is within its class,
- 03:34 you can't use it anywhere else.
- 03:35 In the next video, we're going to move into something called class getters.
Lesson notes are only available for subscribers.