# In Depth: attr\_reader/writer/accessor

**URL:** <https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958>\
**Category:** Ruby\
**Created:** [November 23, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958 "2008-11-23T22:50:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mansfiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mansfiem](https://www.ruby-forum.com/u/mansfiem)\
**Post date:** [November 23, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958/1 "2008-11-23T22:50:10Z")

</div>

Can anyone explain exactly what happens when the interpreter interprets  
attr\_reader and its kin?

E.G. where is the method located (in which module, class) and what does  
it do with the symbols in order to expand the method call into an  
instance method of the calling class object?

---

<div class="post-metadata">

**Author:** ![mansfiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mansfiem](https://www.ruby-forum.com/u/mansfiem)\
**Post date:** [November 23, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958/2 "2008-11-23T23:32:38Z")

</div>

On Sun, Nov 23, 2008 at 4:45 PM, Matthew M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Can anyone explain exactly what happens when the interpreter interprets  
> attr\_reader and its kin?
> 
> E.G. where is the method located (in which module, class) and what does  
> it do with the symbols in order to expand the method call into an  
> instance method of the calling class object?

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241709](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241709)

HTH  
Michael G.

---

<div class="post-metadata">

**Author:** ![mansfiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mansfiem](https://www.ruby-forum.com/u/mansfiem)\
**Post date:** [November 24, 2008, 9:44am UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958/3 "2008-11-24T09:44:17Z")

</div>

Hi –

On Mon, 24 Nov 2008, Matthew M. wrote:

> Can anyone explain exactly what happens when the interpreter interprets  
> attr\_reader and its kin?
> 
> E.G. where is the method located (in which module, class) and what does  
> it do with the symbols in order to expand the method call into an  
> instance method of the calling class object?

attr\_reader and friends are private instance methods of the class  
Module:

> > Module.private\_instance\_methods(false).grep(/attr/)  
> > =\> [“attr\_accessor”, “attr\_writer”, “attr\_reader”, “attr”]

which means that as long as self is an instance of Module or one of  
its subclasses, those methods can be called. The Class class is a  
subclass of Module, so instances of Class can execute those methods  
too. That’s what’s happening when you do:

class C  
attr\_accessor :x  
end

As for what it does: see Greg’s Ruby implementation, and also the  
original C code which is in eval.c (look for the definition of  
rb\_attr). It basically calls rb\_define\_method, which is a general  
method for adding methods to classes.

I used implementing attr\_\* as a metaprogramming exercise in a Ruby  
course I taught last week, and it was very instructive. Amazing what  
you can do in Ruby, without even getting terribly convoluted or  
opaque.

David

---

<div class="post-metadata">

**Author:** ![mansfiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mansfiem](https://www.ruby-forum.com/u/mansfiem)\
**Post date:** [November 24, 2008, 10:03am UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958/4 "2008-11-24T10:03:31Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Mon, 24 Nov 2008, Matthew M. wrote:
> 
> > Can anyone explain exactly what happens when the interpreter interprets  
> > attr\_reader and its kin?
> > 
> > E.G. where is the method located (in which module, class) and what does  
> > it do with the symbols in order to expand the method call into an  
> > instance method of the calling class object?
> 
> attr\_reader and friends are private instance methods of the class  
> Module:
> 
> > > Module.private\_instance\_methods(false).grep(/attr/)  
> > > =\> [“attr\_accessor”, “attr\_writer”, “attr\_reader”, “attr”]
> 
> which means that as long as self is an instance of Module or one of  
> its subclasses, those methods can be called. The Class class is a  
> subclass of Module, so instances of Class can execute those methods  
> too. That’s what’s happening when you do:
> 
> class C  
> attr\_accessor :x  
> end
> 
> As for what it does: see Greg’s Ruby implementation, and also the  
> original C code which is in eval.c (look for the definition of  
> rb\_attr). It basically calls rb\_define\_method, which is a general  
> method for adding methods to classes.
> 
> I used implementing attr\_\* as a metaprogramming exercise in a Ruby  
> course I taught last week, and it was very instructive. Amazing what  
> you can do in Ruby, without even getting terribly convoluted or  
> opaque.
> 
> David

Not that I don’t appreciate the other responses but this is exactly what  
I was looking for. Thank you for your help everyone. Also, David, can  
you recommend any reference texts for Ruby that delve into this much  
detail for other similar concepts. I’m nearly finished with Programming  
Ruby: Pragmatic Programmers and I feel like I want a bit more.

Thanks!

---

<div class="post-metadata">

**Author:** ![mansfiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mansfiem](https://www.ruby-forum.com/u/mansfiem)\
**Post date:** [November 24, 2008, 12:04am UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958/5 "2008-11-24T00:04:14Z")

</div>

Not an answer to the OP’s question, but…

On Sun, Nov 23, 2008 at 5:27 PM, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Sun, Nov 23, 2008 at 4:45 PM, Matthew M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241709](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241709)

This implementation:

> def new\_attr\_reader cl, sym  
> str = “def #{sym.to\_s}; @#{sym.to\_s}; end”  
> cl.class\_eval str  
> end

is more dangerous than it needs to be. Instead, you can write:

def new\_attr\_reader(\*args)  
args.each { |e| define\_method(e) { instance\_variable\_get(“@#{e}”) } }  
end

Which allows for:

class A  
new\_attr\_reader :a, :b, :c

def initialize  
@a = 1  
@b = 2  
@c = 3  
end  
end

a = A.new  
p [a.a, a.b, a.c] #=\> [1, 2, 3]

Of course, substitute instance\_variable\_set for writers.

---

<div class="post-metadata">

**Author:** ![mansfiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mansfiem](https://www.ruby-forum.com/u/mansfiem)\
**Post date:** [January 19, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/in-depth-attr-reader-writer-accessor/153958/6 "2009-01-19T18:12:41Z")

</div>

Hi –

On Mon, 24 Nov 2008, Matthew M. wrote:

> > > instance method of the calling class object?  
> > > too. That’s what’s happening when you do:  
> > > I used implementing attr\_\* as a metaprogramming exercise in a Ruby  
> > > detail for other similar concepts. I’m nearly finished with Programming  
> > > Ruby: Pragmatic Programmers and I feel like I want a bit more.

Sorry – this got lost in the inbox pipeline. I have an answer for  
you, though: The Well-Grounded Rubyist (by me), due out in  
March. See [The Well-Grounded Rubyist](http://www.manning.com/black2). There’s no one-stop-shopping  
source for all such stuff but I think you’ll find a good bit in there.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!
