# Newbie: class attribute accessors?

**URL:** <https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259>\
**Category:** Ruby\
**Created:** [October 23, 2006, 11:36am UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259 "2006-10-23T11:36:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 11:36am UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/1 "2006-10-23T11:36:12Z")

</div>

Hello,  
I was wondering if it is possible to get the automatically generated  
accessors (attr\_accessor, etc…) for class attributes. From what I  
found on the internet, it seems that the usual syntax wouldn’t work.

Thanks.

Diego V.

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 11:56am UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/2 "2006-10-23T11:56:35Z")

</div>

Diego V. wrote:

> Hello,  
> I was wondering if it is possible to get the automatically generated  
> accessors (attr\_accessor, etc…) for class attributes. From what I  
> found on the internet, it seems that the usual syntax wouldn’t work.

Please explain. Use an example.

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/3 "2006-10-23T12:26:54Z")

</div>

> Please explain. Use an example.

> From what I understood, for any instance variable, I can make ruby  
> automatically produce the accessors. For example:

class Dog  
@name  
@@NumberOfLegs = 4

attr\_accessor :name  
end

x = Animal.new  
…  
puts x.name

However, I would like to do something similar for class variables. So  
for example in the above example, for @@NumberOfLegs.

Is this possible?

Thank you

Diego V.

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/4 "2006-10-23T13:24:26Z")

</div>

Hi –

On Mon, 23 Oct 2006, Diego V. wrote:

> end
> 
> x = Animal.new  
> …  
> puts x.name
> 
> However, I would like to do something similar for class variables. So  
> for example in the above example, for @@NumberOfLegs.
> 
> Is this possible?

See Farrel’s answer; you can write wrapper methods. Note, however,  
that these are not exactly “attributes”. Class variables are shared  
among many different objects (all the classes in a hierarchy; all the  
instances of all those classes), so they can’t represent an attribute  
in the normal/usual sense.

You can certainly give your class objects attributes. Just call  
attr\_accessor in the object’s singleton class:

class Dog  
class \<\< self  
attr\_accessor :number\_of\_legs  
end  
self.number\_of\_legs = 4  
end

puts Dog.number\_of\_legs # =\> 4  
Dog.number\_of\_legs = 10 # well, maybe attr\_reader is better 🙂

David

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/5 "2006-10-23T12:36:11Z")

</div>

On 23/10/06, Diego V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Thank you
> 
> Diego V.

I don’t know if there is an ‘attr\_accessor’ shortcut but you can do it  
manually  
irb(main):001:0\> class Dog  
irb(main):002:1\> def self.number\_of\_legs  
irb(main):003:2\> @@numberOfLegs  
irb(main):004:2\> end  
irb(main):005:1\> def self.number\_of\_legs=(value)  
irb(main):006:2\> @@numberOfLegs = value.to\_int  
irb(main):007:2\> end  
irb(main):008:1\> end  
=\> nil  
irb(main):009:0\> Dog.number\_of\_legs=5  
=\> 5  
irb(main):010:0\> Dog.number\_of\_legs  
=\> 5

Farrel

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/6 "2006-10-23T13:26:26Z")

</div>

Diego V. wrote:

/ …

> However, I would like to do something similar for class variables. So  
> for example in the above example, for @@NumberOfLegs.
> 
> Is this possible?

I don’t think so (and I am certainly open to correction), and I think I  
know  
the reason. A class variable belongs to the class, not to any particular  
instance. The accessor shortcuts are meant to make instance variables  
available, but you need to think what would happen if any instance could  
change the class variables.

Class variables are meant to hold values the entire class hold in  
common.  
These variables should not ordinarily be modifiable by any particular  
instance. Also, if accessor functions existed for class variables, this  
would essentially allow them to be modified globally, by anyone, without  
any of the usual protections. Further, such an accessor feature would  
exist  
at the class level, just like the variables it modified, so no instance  
would need to be created.

Again, I could be wrong about this.

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/7 "2006-10-23T13:26:26Z")

</div>

Diego V. wrote:

> end
> 
> Thank you
> 
> Diego V.

You can do this for instance variables of a class, not sure there are  
equivalents for actual class variables though:

class Dog  
@names = [“Fido”, “Rex”]  
class \<\< self  
attr\_accessor :names  
end  
end

puts Dog.names

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [October 23, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/newbie-class-attribute-accessors/73259/8 "2006-10-23T18:39:53Z")

</div>

On 2006.10.23 20:25, John T. wrote:

> > attr\_accessor :name  
> > Is this possible?  
> > @names = [“Fido”, “Rex”]  
> > class \<\< self  
> > attr\_accessor :names  
> > end  
> > end  
> > puts Dog.names

Please note that it is often better to use class instance variables  
(@var at the class scope) rather than class variables (@@var):

class A; @@a = 5; end  
class B \< A; @@a = 4; end  
class C \< A; @@a = 3; end  
class D \< B; @@a = 2; end

p A.send ‘class\_variable\_get’, “@@a”  
p B.send ‘class\_variable\_get’, “@@a”  
p C.send ‘class\_variable\_get’, “@@a”  
p D.send ‘class\_variable\_get’, “@@a”

class C; @@a = 4; end

p A.send ‘class\_variable\_get’, “@@a”  
p B.send ‘class\_variable\_get’, “@@a”  
p C.send ‘class\_variable\_get’, “@@a”  
p D.send ‘class\_variable\_get’, “@@a”

Class instance variables have a simpler inheritance, none:

class E; @a = 1; end  
class F \< E; end

p E.send ‘instance\_variable\_get’, ‘@a’  
p F.send ‘instance\_variable\_get’, ‘@a’

class F; @a = 2; end

p E.send ‘instance\_variable\_get’, ‘@a’  
p F.send ‘instance\_variable\_get’, ‘@a’
