If you’ve been using Ruby for a while, you’ve almost certainly defined a class
with one of
attr_accessor. They’re used very
widely in Ruby codebases, and are in many tutorials. In this blog, we’ll
breakdown this simple but powerful syntactical sugar, and see what it actually
Here’s an example class with an
attr_reader. We use this when we need an
attribute that should only be changed from private methods, but whose value
still needs to be available publicly.
It’s used like this:
The example above is essentially shorthand for:
attr_writer, only the setter method is defined. The getter method is left
out. Maybe we have a secret variable we don’t want other objects to be able to
read. We want to be able to set it though.
Without the syntactical sugar in the above example, you’d have to write out something like this:
Lastly, let’s take a look at
attr_accessor. It doesn’t do anything you
haven’t already seen, it just creates the setter and getter methods as seen
above. Let’s make use of
attr_accessor for our
Without syntactical sugar we would have to do this:
So, in summary:
attr_*methods are syntactical sugar that create setter and getter methods
- A setter method allows users to set the value of an instance variable on an object.
- A getter method allows users to get the value of an instance variable from an object.
attr_readercreates just the getter.
attr_writercreates just the setter.
attr_accessorcreates both setter and getter.