Accessor in Ruby

Juzer Shakir
4 min readJun 26, 2022

A built-in method that provides getter and setter methods.

Before we get into what an Accessor is and its methods and how they’re defined in a class, let's understand the motivation behind it.

Source http://www.scaler.com/

Let’s create a class named student where we will store basic information about each student such as name, grade & roll number.

Now, to access the name of any Student class object we cannot access it as an instance method, let’s try out:

To work around this error we can create a method inside the Student class with the same name as an instance variable:

And hence, we can create similar methods for other instance variables too and access it as an instance method.

But now let's say we wanted to set a new roll number for any student or modify the existing roll number value, we could do something like this:

Nope, that didn’t work out. It expects us to have a method name like roll_number= in order to set a new value, so let’s create a method in the class with that name:

And we get the expected result. However, if we wanted to extract and modify all the instance variables then we would need to define setter and getter methods for all of them, and following this strategy our code would no longer follow the DRY principle as we’re repeating ourselves to achieve similar functionality for all instance variables.

To avoid this and follow the DRY principle, Ruby provides us with a method called accessors. So what is an Accessor?

Accessors are a way to create getter and setter methods without explicitly defining them in a class.

There are 3 types of Accessor methods:

  1. attr_reader
  2. attr_writer
  3. attr_accessor

attr_reader

Variables declared by the attr_reader method has only getter methods available to them which get a value. For example, let's try this on our Student class example:

We can give the names of the variables to the attr_reader method for which we would like to access the value. Basically, providing getter methods for us which is why we were able to access the name of that student.

attr_writer

Variables declared by theattr_writer method has only setter methods available to them which set a value.

The name variable doesn’t have the setter method which means we cannot change its value once an instance has been created.

So now basically we have setter and getter methods for both instance variables except for one.

attr_accessor

Variables declared by attr_accessor, both have getter and setter methods available to them. Hence, we can access and set new values to the class object.

attr_accessor helps us to prevent mentioning the same instance variables to each of the accessor functions, since it provides us with both setter and getter methods.

Now if we try to access a getter or setter method that isn’t available, we would get the same error as we saw above. For example:

An id is an auto-generated number from 1 to 10,000 for each instance or object of the class Student.

Here, we have only set the getter method of id with attr_reader, that’s why we were able to read the value but not set it manually.

And yeah, that’s it! I hope the information was valuable to you. Any suggestions are always welcome!

--

--