Locked lesson.
About this lesson
How to create a class setter and why you should.
Exercise files
Download this lesson’s related exercise files.
Class Setters.docx59 KB Class Setters - Solution.docx
59.6 KB
Quick reference
Class Setters
To set things in a class, use a setter.
When to use
Use a setter whenever you create a getter in a class.
Instructions
To create a setter in a class:
class Square
def initialize(side_length)
@side_length = side_length
end
# Create Getter
def side_length
return @side_length
end
# Create Setter
def side_length=(side_length)
@side_lenght = side_length
end
end
Hints & tips
- Setters let you set things in a class.
- To name your setter method, use your same instance variable name, but put an equal sign on it.
- 00:04 In the last video, we looked at getters, in this video, I want to look at setters,
- 00:09 and that's just S-E-T-T-E-R-S.
- 00:11 So you can imagine what a setter does if a getter gets something,
- 00:14 a setter sets something.
- 00:16 So in our little class here, all we have here is this side length.
- 00:20 And when we create our instance, when we instantiate
- 00:24 our class at the beginning here with the mysquare = square.new with a 20,
- 00:28 we're telling it right now, pass in 20 as the side length.
- 00:32 And that's fine, later on we can use our getter to return that value if we want.
- 00:37 But what if later on, we change our mind and we want to set it to something else?
- 00:41 Well, you need to use a setter for that, so
- 00:43 you need to define a setter as one of your methods inside your class.
- 00:47 So it's pretty simple, it's just like all the other methods we've created.
- 00:51 You start with define and then you name it.
- 00:54 So again, you want to use the same name that you used as your parameter up there.
- 00:59 So side_length,
- 01:01 the difference is because we've already used side_length as a getter right here.
- 01:05 We've already named this method, you can't have two methods with the same names, but
- 01:09 you put an equal to sign because we are assigning something new to side_length.
- 01:14 Remember, our assignment operator from way back at the beginning of the course,
- 01:17 same thing here we're assigning something new, but
- 01:19 we add it right to the name of the method.
- 01:22 Don't do that with the space that won't work,
- 01:25 it has to actually be touching, right?
- 01:26 So then we pass in the parameter, it's the same thing,
- 01:30 we want to do side length.
- 01:32 Keep the parameter name the same throughout.
- 01:34 And now we can end our method, and inside of here
- 01:39 we just set @side_length = side_length.
- 01:45 So this side_length is this
- 01:49 side_length's parameter that we're passing into our setter.
- 01:53 And our setter is called side_length=, you could probably do that,
- 01:58 that would work too.
- 01:59 I tend to them together just to remind myself that this is a setter, and
- 02:02 in fact, I might
- 02:06 use a comment here, because we're starting to get a little complicated here,
- 02:09 things are starting to run together.
- 02:13 All right?
- 02:15 So okay, so we've got our setter created, how do we use it?
- 02:18 Well, we come down here and we've instantiated our class.
- 02:22 And if I comment that out, all we have to do is go
- 02:25 my_square.side_length =, let's say, 5.
- 02:32 So actually, let's go puts my_square and that's puts it again beneath it.
- 02:39 So we're going to start out with our class, and our sides are going to
- 02:43 be 20 each, and then we're going to put that out, we'll see if that's true.
- 02:47 And then we're going to change it, we're going to set it to 5.
- 02:50 Save this and see if it works correctly.
- 02:52 So your square has a side length of 20.
- 02:55 And then we set it to 5, and output it, your square now has a side length of 5, so
- 02:59 we can change these things on the fly in the future.
- 03:02 We can do it from outside of our class, but we do it with this setter.
- 03:06 So those are setters, those are getters, in the next video,
- 03:09 we're going to talk about the attr_accessor.
Lesson notes are only available for subscribers.