# Attr\_reader explained

**URL:** <https://rubytalk.org/t/attr-reader-explained/35834>\
**Category:** ruby-talk\
**Created:** [3 March 2007 17:55 UTC](https://rubytalk.org/t/attr-reader-explained/35834 "2007-03-03T17:55:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![libsfan01](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@libsfan01](https://rubytalk.org/u/libsfan01)\
**Post date:** [3 March 2007 17:55 UTC](https://rubytalk.org/t/attr-reader-explained/35834/1 "2007-03-03T17:55:04Z")

</div>

can someone explain how attr\_reader works?

i can't find a good explanation anywhere.

please help!

---

<div class="post-metadata">

**Author:** ![Tim\_Becker](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@Tim\_Becker](https://rubytalk.org/u/Tim_Becker)\
**Post date:** [3 March 2007 18:14 UTC](https://rubytalk.org/t/attr-reader-explained/35834/2 "2007-03-03T18:14:48Z")

</div>

> can someone explain how attr\_reader works?
> 
> i can't find a good explanation anywhere.
> 
> please help!

It's built in to the language, so it 'works' by magic for all  
practical purposes, it's implemented in `object.c` if you want to take  
a look at the implementation. If you wanted to implement it in Ruby,  
you do something like this:

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

> class Test; end  
> new\_attr\_reader Test, :my\_new\_reader  
> Test.new().my\_new\_reader

The built in `attr_reader` takes an array of sym's, but you get the idea.  
&nbsp;&nbsp;&nbsp;-tim

> **···**
>
> On 3/3/07, libsfan01 \<mcyi2mr3@googlemail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Raj\_Sahae](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/raj_sahae/32/1840_2.png) [@Raj\_Sahae](https://rubytalk.org/u/Raj_Sahae)\
**Post date:** [3 March 2007 18:21 UTC](https://rubytalk.org/t/attr-reader-explained/35834/3 "2007-03-03T18:21:19Z")

</div>

libsfan01 wrote:

> can someone explain how attr\_reader works?
> 
> i can't find a good explanation anywhere.
> 
> please help!  
> &nbsp;&nbsp;

attr\_reader, in it's common usage, creates instance variables and defines a method by which you can read them.

Class Test  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(num)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@test=num  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@test  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

If you were to use attr\_reader instead, your class definition would be

Class Test  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :test  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(num)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@test = num  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [3 March 2007 19:47 UTC](https://rubytalk.org/t/attr-reader-explained/35834/4 "2007-03-03T19:47:31Z")

</div>

Alle sabato 3 marzo 2007, Raj Sahae ha scritto:

> attr\_reader, in it's common usage, creates instance variables and  
> defines a method by which you can read them.

Actually, attr\_reader doesn't create the variable, just the accessor method.  
To see this, do the following in irb:

> class C  
> attr\_reader :var  
> def var\_defined?  
> defined?(@var)  
> end  
> end

=\> nil

> c=C.new

=\> #\<C:0xb7a4c650\>

> c.var\_defined?

=\> nil

> class C  
> def initialize  
> @var=nil  
> end  
> end

=\> nil

> c1=C.new

=\> #\<C:0xb7cf1a04 @var=nil\>

> c1.var\_defined?

=\> "instance-variable"

As you can see, in the first case (the variable called c) doesn't have the  
@var instance variable defined. Even after you call the var method defined  
using attr\_reader, the variable is still not defined. To define it, you need  
to explicitly assign it to a value (using @var= something inside an instance  
method of the class or using instance\_variable\_set)

Stefano

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [3 March 2007 23:25 UTC](https://rubytalk.org/t/attr-reader-explained/35834/5 "2007-03-03T23:25:52Z")

</div>

> \> can someone explain how attr\_reader works?  
> \> i can't find a good explanation anywhere.  
> It's built in to the language, so it 'works' by magic for all  
> practical purposes, it's implemented in `object.c` if you want to take  
> a look at the implementation. If you wanted to implement it in Ruby,  
> you do something like this:

Actually, that's not true. It's written in C, but that's not the same  
as being built into the language. It's a method on Module, so it \*can\*  
be overridden.

> > class Module  
> > &nbsp;&nbsp;alias old\_attr\_reader attr\_reader  
> > &nbsp;&nbsp;def attr\_reader(\*names)  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Making attribute readers for #{names.join(", ")}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;old\_attr\_reader \*names  
> > &nbsp;&nbsp;end  
> > end

=\> nil

> > class Foo  
> > &nbsp;&nbsp;attr\_reader :bar  
> > &nbsp;&nbsp;attr\_reader :baz, :quux  
> > end

Making attribute readers for bar  
Making attribute readers for baz, quux

-austin

> **···**
>
> On 3/3/07, Tim Becker \<a2800276@gmail.com\> wrote:
> 
> > On 3/3/07, libsfan01 \<mcyi2mr3@googlemail.com\> wrote:
> 
> --  
> Austin Ziegler \* halostatue@gmail.com \* [http://www.halostatue.ca/](http://www.halostatue.ca/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@halostatue.ca \* [You are in a maze of twisty little passages, all alike. // halo • statue](http://www.halostatue.ca/feed/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@zieglers.ca

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [5 March 2007 21:28 UTC](https://rubytalk.org/t/attr-reader-explained/35834/6 "2007-03-05T21:28:49Z")

</div>

attr\* also cheats, so methods defined by attr\* are slightly faster than methods defined with def:

[http://blog.segment7.net/articles/2006/03/06/attr-vs-method-vs-define\_method](http://blog.segment7.net/articles/2006/03/06/attr-vs-method-vs-define_method)

> **···**
>
> On Mar 3, 2007, at 15:25, Austin Ziegler wrote:
> 
> > On 3/3/07, Tim Becker \<a2800276@gmail.com\> wrote:
> > 
> > > On 3/3/07, libsfan01 \<mcyi2mr3@googlemail.com\> wrote:  
> > > \> can someone explain how attr\_reader works?  
> > > \> i can't find a good explanation anywhere.  
> > > It's built in to the language, so it 'works' by magic for all  
> > > practical purposes, it's implemented in `object.c` if you want to take  
> > > a look at the implementation. If you wanted to implement it in Ruby,  
> > > you do something like this:
> > 
> > Actually, that's not true. It's written in C, but that's not the same  
> > as being built into the language. It's a method on Module, so it \*can\*  
> > be overridden.
