ruby – attr_accessor, attr_reader, and attr_writer

Recently, I started working on Ruby and JRuby. Good to start posting some tutorials.

You may use the different accessors to communicate your intent to someone reading your code, and make it easier to write classes which will work correctly no matter how their public API is called.

1
2
3
4
5
class 
attr_accessor :age

...
end

Here, I can see that I may both read and write the age.

1
2
3
4
5
class 
attr_reader :age

...
end

Here, I can see that I may only read the age. Imagine that it is set by the constructor of this class and after that remains constant. If there were a mutator (writer) for age and the class were written assuming that age, once set, does not change, then a bug could result from code calling that mutator.

But what is happening behind the scenes?

If you write:

1
2
3
4
5
class Clazz
attr_writer :age

...
end

That gets translated into:

1
2
3
4
5
6
7
class Clazz
def age=(value)
@age = value
end

...
end

If you write:

1
2
3
4
5
class Clazz
attr_reader :age

...
end

That gets translated into:

1
2
3
4
5
6
7
class Clazz
def age
@age
end

...
end

If you write:

1
2
3
4
5
class Clazz
attr_accessor :age

...
end

That gets translated into:

1
2
3
4
5
6
7
8
9
10
11
class Clazz
def age=(value)
@age = value
end

def age
@age
end

...
end

Knowing that, here’s another way to think about it: If you did not have the attr_... helpers, and had to write the accessors yourself, would you write any more accessors than your class needed? For example, if age only needed to be read, would you also write a method allowing it to be written?


References: