# Newbie question about attr\_reader

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160>\
**Category:** Ruby\
**Created:** [November 16, 2006, 10:27am UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160 "2006-11-16T10:27:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravi\_rao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5e9695/32.png) [@ravi\_rao](https://www.ruby-forum.com/u/ravi_rao)\
**Post date:** [November 16, 2006, 10:27am UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160/1 "2006-11-16T10:27:09Z")

</div>

Hi,  
Why cant i have an attribute reader for a class variable.  
Here’s a program.

class SomeClass

@@var1 = 9 #class var  
def someMethod  
@@var1 = 1  
end

def initialize  
@var2 = 5  
end

def getVar1  
return @@var1  
end

attr\_reader :var2  
attr\_reader :var1

end #SomeClass ends

s1 = SomeClass.new  
puts(s1.var2)  
puts(s1.getVar1)  
s1.someMethod  
puts(s1.var2)  
puts(s1.getVar1)  
puts(s1.var1)

Here’s the output:

5  
9  
5  
1  
nil

Thanks,  
Ravi Rao

---

<div class="post-metadata">

**Author:** ![ravi\_rao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5e9695/32.png) [@ravi\_rao](https://www.ruby-forum.com/u/ravi_rao)\
**Post date:** [November 16, 2006, 10:47am UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160/2 "2006-11-16T10:47:10Z")

</div>

On 11/16/06, ravi rao [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> Why cant i have an attribute reader for a class variable.  
> Thanks,  
> Ravi Rao

The reason being that in Ruby, instance variables are exposed to the  
outside  
world via one of the attr family of methods.

---

<div class="post-metadata">

**Author:** ![ravi\_rao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5e9695/32.png) [@ravi\_rao](https://www.ruby-forum.com/u/ravi_rao)\
**Post date:** [November 16, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160/3 "2006-11-16T13:01:34Z")

</div>

Hi –

On Thu, 16 Nov 2006, ravi rao wrote:

> Hi,  
> Why cant i have an attribute reader for a class variable.

Class variables are shared among many objects (the class, its  
subclasses, all instances of all those classes), so they’re not really  
the right choice for representing an “attribute”, which is generally  
understood to be a property of a particular object.

You can certain write wrapper get/set methods around class variables,  
and even automate the process. You can also give your classes their  
own attributes; since classes are objects, they can have attributes  
like other objects. To do that, you need to call attr\_\* in the  
context of the class’s singleton class:

class C  
class \<\< self  
attr\_accessor :x  
end  
end

Now, the class object C has an attribute “x”:

C.x = 1  
puts C.new.class.x # 1

etc.

David

---

<div class="post-metadata">

**Author:** ![ravi\_rao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5e9695/32.png) [@ravi\_rao](https://www.ruby-forum.com/u/ravi_rao)\
**Post date:** [November 16, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160/4 "2006-11-16T14:15:47Z")

</div>

Hi

The reason is because you are exposing @var1 not @@var1 and with  
someMethod you are initializing @@var1 not @var1, so it’s nil.

Kind regards

On Thu, 16 Nov 2006 18:26:40 +0900  
“ravi rao” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> attr\_reader :var1  
> puts(s1.var1)
> 
> Thanks,  
> Ravi Rao

* * *

LLama Gratis a cualquier PC del Mundo.  
Llamadas a fijos y móviles desde 1 céntimo por minuto.  
[http://es.voice.yahoo.com](http://es.voice.yahoo.com)

---

<div class="post-metadata">

**Author:** ![ravi\_rao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5e9695/32.png) [@ravi\_rao](https://www.ruby-forum.com/u/ravi_rao)\
**Post date:** [November 17, 2006, 10:49am UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160/5 "2006-11-17T10:49:34Z")

</div>

On Nov 16, 2006, at 1:01 PM, removed\_email\_address@domain.invalid wrote:

> class C  
> class \<\< self  
> attr\_accessor :x  
> end  
> end
> 
> Now, the class object C has an attribute “x”:
> 
> C.x = 1  
> puts C.new.class.x # 1

David, technically is there any relationship between the attribute @x  
here

class A  
@x = 1  
end

and the @x that accessor deals with? That would be an attribute of  
what? Is there a singleton instance of the singleton class? Or is  
that @x an attribute of A as instance of Class? I tried to get this  
right with examples but I guess the exact answer comes from the  
actual implementation.

– fxn

fxn@feynman:~/tmp$ cat foo.rb  
class A  
@x = 0  
@y = 1  
class \<\< self  
def x  
@x  
end  
attr\_accessor :y  
end  
end

puts A.x, A.y  
fxn@feynman:~/tmp$ ruby foo.rb  
0  
1

---

<div class="post-metadata">

**Author:** ![ravi\_rao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5e9695/32.png) [@ravi\_rao](https://www.ruby-forum.com/u/ravi_rao)\
**Post date:** [November 17, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/newbie-question-about-attr-reader/76160/6 "2006-11-17T12:09:41Z")

</div>

Hi –

On Fri, 17 Nov 2006, Xavier N. wrote:

> > C.x = 1  
> > puts C.new.class.x # 1
> 
> David, technically is there any relationship between the attribute @x here
> 
> class A  
> @x = 1  
> end
> 
> and the @x that accessor deals with?

Yes; they’re the same @x.

> That would be an attribute of what? Is there a singleton instance of  
> the singleton class? Or is that @x an attribute of A as instance of  
> Class? I tried to get this right with examples but I guess the exact  
> answer comes from the actual implementation.

@x is an instance variable of A. The accessor methods are defined for  
A only, not for all classes. In general, when you do this:

class \<\< obj  
…  
end

you’re in the singleton class of obj, and whatever instance methods  
you define are singleton methods of obj. attr\_accessor is just a  
mechanism for writing instance methods. So what I did was equivalent  
to:

class A  
class \<\< self  
def x  
@x  
end  
def x=(y)  
@x = y  
end  
end  
end

i.e., adding instance methods to A’s singleton class.

David
