Locked lesson.
About this lesson
What is a getter and how do we use it?
Exercise files
Download this lesson’s related exercise files.
Class Getters.docx59 KB Class Getters - Solution.docx
59.5 KB
Quick reference
Class Getters
Getters let you get things from your class.
When to use
Use them whenever you create any class.
Instructions
To create a getter:
class Square
def initialize(side_length)
@side_length = side_length
end
# Create Getter
def side_length
return @side_length
end
end
Hints & tips
- Getters let you get stuff from your class
- Getters should be named the same as their instance variable
- 00:04 In this video I want to talk about getters.
- 00:06 So with classes you have something called a getter and a setter, and
- 00:10 it's just getter, G-E-T-T-E-R-S.
- 00:12 And a getter allows you to get something from your class, right?
- 00:17 So far we've initialized, that we've passed in our side length, but
- 00:20 we can't really do anything with it.
- 00:22 We need to set a getter, create a getter method in order to be able to use that
- 00:27 side length outside of our program, right?
- 00:29 So to do that, we just create another method, and you generally want
- 00:34 to name your getter the same name as whatever the variable thing is,
- 00:39 the parameter that you passed into it.
- 00:42 So we're going to go side_length, okay?
- 00:47 And generally what you're going to want to do with a getter is just return
- 00:52 your instance variable of side_length or
- 00:55 whatever parameter you're looking to turn into a getter.
- 01:00 So if we save this now, so here's where we initialize our square and
- 01:04 we've passed in 20.
- 01:06 Now, to use that 20 for something in the future,
- 01:11 we can call puts my_square.side_length.
- 01:15 As I start to type it already realizes, this method exists and we can call it.
- 01:19 Now, the side_length is now a method of the square class, right?
- 01:23 So if we save this and hit Reload, boom we get 20.
- 01:27 Likewise if we change this to 10, we get 10.
- 01:31 So you can see this adding a dot and putting method on the end, the very object
- 01:36 orientee type of thing and getters are very, very fundamental to classes.
- 01:41 And in the next video we're going to talk about setters.
- 01:43 You can guess what they do but not too difficult of a concept to wrap your brain
- 01:48 around this getter. And we can start to play around
- 01:53 with these things and sort of add functionality to our class.
- 01:56 What else can you do with a square?
- 01:58 Let's call the area.
- 01:59 So define area.
- 02:03 And how do you find the area of a square?
- 02:04 It's side length times side length, right?
- 02:07 So we can go return @side_length
- 02:13 times @side_length and that works.
- 02:20 So now we can save this and come down here and type in area.
- 02:26 We save it, boom, our area is 100 because 10 times 10 is 100.
- 02:31 If we change our side length to 20, 400,
- 02:34 sort of off topic from a getter, but I just flesh this out a little bit more.
- 02:39 You can do parameter, you can do whatever different things you want your square
- 02:43 class to be able to do, and it's just really cool.
- 02:46 So that's getters, you generally want to create a getter for
- 02:49 all of your parameters that you pass in.
- 02:52 In the next video, we're going to look at setters.
Lesson notes are only available for subscribers.