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
Lesson notes are only available for subscribers.