# Is using attr\_accessor for class variables possible?

**URL:** <https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212>\
**Category:** ruby-talk\
**Created:** [7 October 2007 12:28 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212 "2007-10-07T12:28:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel\_Dragffy](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@Gabriel\_Dragffy](https://rubytalk.org/u/Gabriel_Dragffy)\
**Post date:** [7 October 2007 12:28 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212/1 "2007-10-07T12:28:39Z")

</div>

I know that attr\_accessor works for accessing instance variables, but it doesn't seem to work for class ones. What is the best way to set/retrieve the value of a class variable?

Many thanks

Gabriel

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 October 2007 12:39 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212/2 "2007-10-07T12:39:24Z")

</div>

Hi --

> I know that attr\_accessor works for accessing instance variables, but it doesn't seem to work for class ones. What is the best way to set/retrieve the value of a class variable?

@@var = 1  
@@var

🙂

If you want to wrap them in methods:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def C.var  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@var  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def C.var=(x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@var = x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

In Rails, there's a thing called "cattr" that does this automatically:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cattr\_acccessor :var  
&nbsp;&nbsp;&nbsp;end

It's a little misleading, though. The term "attribute" (or attr)  
refers to an attribute or property of an object. Class variables are  
very promiscuous: they're shared among many objects (a class, its  
descendants, all instances of all of those classes), so a class  
variable is not really the right choice for an "attribute", and "attr"  
is not the best name for wrappers around class variables.

If you want to represent state per class, the best way is to give your  
class an instance variable or accessor:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self # C's singleton class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :var  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

David

> **···**
>
> On Sun, 7 Oct 2007, Gabriel Dragffy wrote:
> 
> --  
> Upcoming training from Ruby Power and Light, LLC:  
> &nbsp;&nbsp;&nbsp;\* Intro to Ruby on Rails, Edison, NJ, October 23-26  
> &nbsp;&nbsp;&nbsp;\* Advancing with Rails, Edison, NJ, November 6-9  
> Both taught by David A. Black.  
> See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dragffy](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@Gabriel\_Dragffy](https://rubytalk.org/u/Gabriel_Dragffy)\
**Post date:** [7 October 2007 13:04 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212/3 "2007-10-07T13:04:30Z")

</div>

> Hi --
> 
> > I know that attr\_accessor works for accessing instance variables, but it doesn't seem to work for class ones. What is the best way to set/retrieve the value of a class variable?
> 
> @@var = 1  
> @@var

Good point, I mean access the class variable from outside the class (above the class).

> 🙂
> 
> If you want to wrap them in methods:
> 
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def C.var  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@var  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;def C.var=(x)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@var = x  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

I was wondering if I should do this. I just remember attr\_accessor was a short cut for this, but only for instance variables.

> In Rails, there's a thing called "cattr" that does this automatically:
> 
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;cattr\_acccessor :var  
> &nbsp;&nbsp;end
> 
> It's a little misleading, though. The term "attribute" (or attr)  
> refers to an attribute or property of an object. Class variables are  
> very promiscuous: they're shared among many objects (a class, its  
> descendants, all instances of all of those classes), so a class  
> variable is not really the right choice for an "attribute", and "attr"  
> is not the best name for wrappers around class variables.
> 
> If you want to represent state per class, the best way is to give your  
> class an instance variable or accessor:
> 
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self # C's singleton class  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :var  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

I read about singletons in Pragmatic Programming, not entirely sure how to make use of it though

Thank you very much for your advice.

Regards

Gabe

> **···**
>
> On 7 Oct 2007, at 13:39, David A. Black wrote:
> 
> > On Sun, 7 Oct 2007, Gabriel Dragffy wrote:

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [7 October 2007 13:13 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212/4 "2007-10-07T13:13:19Z")

</div>

Hi,

> **···**
>
> Am Sonntag, 07. Okt 2007, 21:39:24 +0900 schrieb David A. Black:
> 
> > If you want to represent state per class, the best way is to give your  
> > class an instance variable or accessor:
> > 
> > &nbsp;&nbsp;class C  
> > &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self # C's singleton class  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :var  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> 
> I do this all the time. But:
> 
> &nbsp;&nbsp;class D \< C ; end
> 
> Always keep in mind that `D.var' will be something different  
> than `C.var'.
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 October 2007 13:56 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212/5 "2007-10-07T13:56:54Z")

</div>

Hi --

> **···**
>
> On Sun, 7 Oct 2007, Bertram Scharpf wrote:
> 
> > Hi,
> > 
> > Am Sonntag, 07. Okt 2007, 21:39:24 +0900 schrieb David A. Black:
> > 
> > > If you want to represent state per class, the best way is to give your  
> > > class an instance variable or accessor:
> > > 
> > > &nbsp;&nbsp;class C  
> > > &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self # C's singleton class  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :var  
> > > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;end
> > 
> > I do this all the time. But:
> > 
> > class D \< C ; end
> > 
> > Always keep in mind that `D.var' will be something different  
> > than `C.var'.
> 
> That's the point of attr\_accessor: to give easy access to per-object  
> state.
> 
> David
> 
> --  
> Upcoming training from Ruby Power and Light, LLC:  
> &nbsp;&nbsp;&nbsp;\* Intro to Ruby on Rails, Edison, NJ, October 23-26  
> &nbsp;&nbsp;&nbsp;\* Advancing with Rails, Edison, NJ, November 6-9  
> Both taught by David A. Black.  
> See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [7 October 2007 15:28 UTC](https://rubytalk.org/t/is-using-attr-accessor-for-class-variables-possible/41212/6 "2007-10-07T15:28:11Z")

</div>

Hi,

> > > If you want to represent state per class, the best way is to give your  
> > > class an instance variable or accessor:
> > > 
> > > &nbsp;&nbsp;class C  
> > > &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self # C's singleton class  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :var  
> > > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;end
> > 
> > I do this all the time. But:
> > 
> > class D \< C ; end
> > 
> > Always keep in mind that `D.var' will be something different  
> > than `C.var'.
> 
> That's the point of attr\_accessor: to give easy access to per-object  
> state.

About a month ago I found myself doing even this:

&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;@x = "x-default"  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def x ; @x or superclass.x ; end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;class D \< C ; end  
&nbsp;&nbsp;class E \< D ; @x = "x-special" ; end

&nbsp;&nbsp;puts C.x  
&nbsp;&nbsp;puts D.x  
&nbsp;&nbsp;puts E.x

I still don't know what is better: using stable instance  
methods (same value on every call) or doing it this way.

Bertram

> **···**
>
> Am Sonntag, 07. Okt 2007, 22:56:54 +0900 schrieb David A. Black:
> 
> > On Sun, 7 Oct 2007, Bertram Scharpf wrote:
> > 
> > > Am Sonntag, 07. Okt 2007, 21:39:24 +0900 schrieb David A. Black:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)
