Attribute Accessors
Attribute accessors provide us with a streamlined way to define getters and setters for instance variables. Otherwise, they are left private.
Let’s say that you have a class with instance variables and you want to expose them to the outside world:
class Food
def initialize(protein)
# this instance variable cannot be accessed from outside
@protein = protein
end
end
bacon = Food.new(25)
bacon.protein # NoMethodError: undefined method `protein'
To solve this, we'd define a getter and setter:
class Food
# getter
def protein
@protein
end
# setter
def protein=(value)
@protein = value
end
end
Lucky for us, Ruby provides attribute accessors so we don't have to do this by hand.
class Food
attr_accessor :protein, :calories
def initialize(protein, calories)
@protein = protein
@calories = calories
end
end
Above, we are having 4 new methods defined for us:
protein
protein=
calories
calories=
attr_reader
- generate getterattr_writer
- generate setterattr_accessor
- generate getter and setter