Locked lesson.
About this lesson
Let Ruby create your getters and setters automatically with attr_accessor.
Exercise files
Download this lesson’s related exercise files.
Understanding Attr_accessor.docx60.4 KB Understanding Attr_accessor - Solution.docx
59.3 KB
Quick reference
Understanding Attr_accessor
Attr_accessor allows you to create getters and setters automatically without typing them out.
When to use
Use them whenever you don't feel like typing out code for getters and setters!
Instructions
To use attr_accessor to create getters and setters:
attr_accessor :side_length
And just add that code to the top of your Class.
Hints & tips
- attr_reader :side_length # Creates a getter
- attr_writer :side_length # Creates a setter
- attr_accessor :side_length # Creates getter and setter
- 00:04 In this video I want to look at attr_accessor,
- 00:10 spelled attr_accessor, attr_accessor.
- 00:15 So looking back at our class, when we start a class, we initialize it and
- 00:20 we pass it this variable, side_length.
- 00:22 Now I should mention,
- 00:23 when we set this thing up we can pass it as many parameters as we want.
- 00:26 And like any other method, we would just, whatever, this, that.
- 00:32 And then we would come down here and
- 00:35 type @whatever = whatever, @this = this, @that = that,
- 00:41 however many of these parameters, that's what we would do.
- 00:46 We're just keeping it simple with just one for our square class but
- 00:49 you can do as many as you want.
- 00:51 But then the main sort of fundamental things in a class,
- 00:54 obviously it starts out with this initialize, then you need a getter and
- 00:57 a setter for whatever parameter.
- 01:00 It's just the fundamental building blocks of a class.
- 01:03 Well, you can see, this is not a whole lot of code here but
- 01:06 we only have one parameter.
- 01:08 If we had 10, or 15, or 100 parameters, we'd have to create getters and
- 01:12 setters for each one and it would be a real hassle.
- 01:15 And they're all the same, right, they all, if it's called side_length the getter is
- 01:19 called side_length, it creates an instance variable called side_length.
- 01:23 The setter is called side_length=, it uses a parameter side_length,
- 01:27 it creates the instance variable side_length, they all stay the same.
- 01:31 They're all very fundamentally the same.
- 01:33 So Ruby, in its infinite wisdom, has created a shortcut so
- 01:37 that we don't have to actually write all of these things.
- 01:39 It was important for
- 01:40 me to explain them to you, that's why I did in the earlier videos.
- 01:43 But we don't have to write them all the time.
- 01:45 In fact, we can just get rid of them.
- 01:47 So there are three ways to do this automatically.
- 01:50 And it's just three lines of code up here at the very top of your class.
- 01:54 So to create, automatically, a getter, we type attr_reader.
- 02:00 I don't why they didn't call it getter but they didn't, and we name it.
- 02:03 So we use that :side_length, that creates, automatically,
- 02:07 all of our getters for the variable, or the parameter side_length.
- 02:12 To set a setter we do attr_writer.
- 02:17 Again, I don't know why they didn't just call it setter, but :side_length.
- 02:21 So these two lines will create a getter and
- 02:25 a setter automatically for our parameter side_length, right?
- 02:31 And that's great but even that is too much, right?
- 02:34 Two whole lines of code, I don't want to have to write that for
- 02:37 every single getter and setter that I want to create.
- 02:39 So there's one more, it's the attr_accessor and
- 02:44 then it's just :side_length.
- 02:47 Now this line will create both a getter and a setter so
- 02:50 you don't have to write those reader/writer things.
- 02:53 They're confusing anyway, which one's the getter and which one's the setter,
- 02:56 I always need to sort of think it through.
- 02:57 With this one right here we can just create both of them in one go.
- 03:01 So now if we run our program again, even though our getters and
- 03:05 our setters are gone, Ruby creates them for us.
- 03:08 And our program still works the way it should.
- 03:10 But now we have much less code, look at this.
- 03:15 Our whole class just jumped down to only this bits of code,
- 03:19 fully equipped with both getters and setters, and we're good to go.
- 03:22 So that's attr_accessor, attr_accessor, however you want to pronounce it.
- 03:27 In the next video will look at class inheritance.
Lesson notes are only available for subscribers.