Changing Ruby Setter & Getter Methods to attr_accessor
In my previous blog of Ruby Getters & Setters Methods, I shared what they are, their syntax, and how to use them. Getter and setter methods are great to read and update information, but they can make your code a bit cluttered. Thankfully there is a cleaner way to get and set information using attr_reader
, attr_writer
, and/or attr_accessor
. The code example for this blog will be a continued from the previous blog I mentioned earlier. I will break down down how attr_reader
, attr_writer
, and attr_accessor
can shorten the length of code when needed to get a value or set a value.
Here is the code from my previous blog. I’ve created a new git repository for this blog for reference as well. There is a student class where each instance of a student will be initialized with a name and a major. The current methods allows us to get a student’s name, get a student’s major, and set a student’s major.
The puts
allow the values to be shown in the console.
Ruby Getter Method and attr_reader
We can shorten the code for our getter methods by using attr_reader
.
Since we want to just get, or read, the student’s name we can add attr_reader: name
before the initialize
method and delete the getter method def name
. The attr_reader :name
is basically a shortcut to writing
def name
@name
end
If you run ruby student.rb
, you’ll see the name “John” and major as “Math” appear. We can make the code cleaner when needing to read the major value as well.
This is done by adding , :major
to theattr_reader
as seen at line 2 in Image C, and deleting the method def major
.
Running the command ruby student.rb
will show the puts statement values of “John” and “Math”.
At the end of the day, Ruby Getter Methods can be done using attr_reader
and :theAttributeToRead
in the format of attr_reader :theAttributeToRead
Ruby Setter Method and attr_writer
Recall that setter methods are a way to update, change, or rewrite a value. We can shorten the code for our setter method by adding attr_writer :major
before the initialize
method and deleting def set_major
as shown in Image D.
The attr_writer :major
is basically a shortcut to writing
def set_major=(major)
@major = major
end
The puts
statements been changed to add a little more clarity of what’s happening for the major
attribute.
Line 12 is still creating an instance of a student as it has always done. Line 13 is puts
ing out the initialized value of “John” when student.name
is called. Line 14 is puts
ing out the value of initialized value of “Math” when student.major
is called. Line 15 is rewriting the value of the attribute major
. Line 16 is puts
ing out the rewritten, or updated value of the attribute major
, which is now “Science”.
At this point, our code is much shorter. Look at the before and after shot below!
So far attr_reader
and attr_writer
act as short cuts for Getter and Setter Methods. What’s interesting is that attr_reader
and attr_writer
have a shortcut as well. Notice how we have the attribute major
in both the attr_reader
and attr_writer
, so we are reading and rewriting the attribute’s value. We can write a single line of code, that is like a shortcut for attr_reader
and attr_writer
, to read and rewrite an attribute’s value. That is done using attr_accessor
.
Ruby Setter & Getter Methods and attr_accessor
Since we want to read and rewrite the student’s major we can add attr_accessor: major
before the initialize
method and delete :major
from attr_reader
and delete attr_writer: major
. The attr_accessor :major
is basically a shortcut to writing
def major
@name
enddef set_major=(major)
@major = major
end
The attr_accessor
may not have seem to saved many lines of code, but it can help in the long run. Any code to reduce vision noise will allow you to focus more on what needs to be done. Come pare the three images before of how the student class was built with Getter and Setter methods, then built with attr_reader
and attr_writer
, then with attr_accessor
.
Quick Summary
Getter methods will get a value for you to read, so the shortcut is attr_reader :the_attribute_to_read
. Setter methods will let you change a value, or re-write a value, so you need an attr_writer :the_attribute_to_rewrite
. If the value needs to be read and re-written, you can use attr_accessor : the_attribute_to_read_or_rewrite
, which will do the jobs of the attr_reader
and attr_writer
.
The git repository for this blog will show the final working code for attr_accessor
and attr_writer
, along with a step by step guide in comments below the code to update the getter/setter methods to attr_reader
, attr_writer
, and/or attr_accessor
. Please feel free to work alongside the guide to get some practice under your fingers.