Getter and Setter Methods in Ruby Explained
What are getters and setters?
Below is an example of a Pet class. A pet has a name and age attribute which are passed in as arguments when we create a new pet object. We want to be able to use the name and age properties later on in the code so we need a way to expose them.
A getter method allows us to access the value of an instance variable, name and age in this example. A setter method allows us to change or set the value of an instance variable, again name and age in this case.
Instance variables are created when a new object is initialized and the initialize method is automatically called. If you don’t have an initialize method or don’t want to pass in an instance variable when an object is created, the setter method can initialize instance variables. Make note though, unlike the initialize method, the setter method is not called automatically.
Two ways to write getter methods:
- With a getter method:
class Pet
def initialize(name, age)
@name=name
@age=age
enddef name #create getter method for name
@name
enddef age #create getter method for age
@age
end
endchester=Pet.new(“Chester”, 5)
chester.name #calling getter method on object returns “Chester”
2. With an attribute reader macro. An attr_reader automatically creates a getter method in one line and allows us to separate attributes with commas. It functions the exact same way as getter. I find this way of writing the getter method is simpler and easier to conceptualize.
class Pet
attr_reader :name, :age #creates getter method for name and agedef initialize(name, age)
@name=name
@age=age
end
endchester=Pet.new(“Chester”, 5)
chester.name #calling getter method on object returns “Chester”
Two ways to write setter methods:
- With a setter method:
class Pet
attr_reader :name, :age #creates getter method for name and agedef initialize(name, age)
@name=name
@age=age
enddef name=(name) #creates a setter method for name
@name=name
enddef age=(age) #creates a setter method for age
@age=age
end
endchester=Pet.new(“Chester”, 5)
chester.age=7 #calling setter method
chester.age #calling getter method returns 7
2. With an attribute writer macro. An attr_writer automatically creates a setter method in one line with attributes separated by commas. It functions the same way as a setter method.
class Pet
attr_reader :name, :age #creates getter method for name and age
attr_writer :name, :age #creates setter methods for name and agedef initialize(name, age)
@name=name
@age=age
end
endchester=Pet.new(“Chester”, 5)
chester.age=7 #calling setter method
chester.age #calling getter method returns 7
Do it all in one step:
If you want all the attributes to be readable and writable, you can refactor even further by writing the getter/reader and setter/writer methods with a single attribute accessor.
class Pet
attr_accessor :name, :age #creates getter and setter methodsdef initialize(name, age)
@name=name
@age=age
end
endchester=Pet.new(“Chester”, 5)
chester.age=7 #calling setter method
chester.age #calling getter method returns 7
The attr_reader, attr_writer, and attr_accessor macros are unique features of Ruby.
Something I learned in my research: When using a setter method within a class, self is required. When using a getter method within a class, self is optional.