# Attr\_reader, etc for class variables

**URL:** <https://rubytalk.org/t/attr-reader-etc-for-class-variables/33612>\
**Category:** ruby-talk\
**Created:** [10 December 2006 05:25 UTC](https://rubytalk.org/t/attr-reader-etc-for-class-variables/33612 "2006-12-10T05:25:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Finnie1](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@Daniel\_Finnie1](https://rubytalk.org/u/Daniel_Finnie1)\
**Post date:** [10 December 2006 05:25 UTC](https://rubytalk.org/t/attr-reader-etc-for-class-variables/33612/1 "2006-12-10T05:25:02Z")

</div>

Is there such a thing?

If I have the following code:  
class Klass  
&nbsp;&nbsp;&nbsp;@@var = 0  
end

How can I make an attr\_writer for it besides doing  
class Klass  
&nbsp;&nbsp;&nbsp;def var=(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@var = other  
&nbsp;&nbsp;&nbsp;end  
end

I've tried all of these to no avail:  
attr\_writer :'Klass.var' # =\> `attr_writer': invalid attribute name `Klass.var' (NameError)

attr\_writer :'var' # =\> undefined method `currentRound=' for Team:Class (NoMethodError) (when I try to use the writer function later)

attr\_writer :'self.class.var' # =\> `attr_writer': invalid attribute name `self.class.var' (NameError)

Any ideas? I mean it's not a huge deal but I would like to find a way around making the method described above.

Thanks,  
Dan

---

<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:** [10 December 2006 07:23 UTC](https://rubytalk.org/t/attr-reader-etc-for-class-variables/33612/2 "2006-12-10T07:23:42Z")

</div>

> Is there such a thing?

No.

> If I have the following code:  
> class Klass  
> &nbsp;&nbsp;@@var = 0  
> end
> 
> How can I make an attr\_writer for it besides doing  
> class Klass  
> &nbsp;&nbsp;def var=(other)  
> &nbsp;&nbsp;&nbsp;&nbsp;@@var = other  
> &nbsp;&nbsp;end  
> end

I tend to use instance variables for this as class variables usually give me hard-to-find bugs.

class Klass  
&nbsp;&nbsp;&nbsp;@var = 0

&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :var  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def some\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.var  
&nbsp;&nbsp;&nbsp;end  
end

p Klass.var # =\> 0  
p Klass.new.some\_method # =\> 0

> **···**
>
> On Dec 9, 2006, at 21:25 , Daniel Finnie wrote:
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)
> 
> I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [10 December 2006 07:31 UTC](https://rubytalk.org/t/attr-reader-etc-for-class-variables/33612/3 "2006-12-10T07:31:56Z")

</div>

Daniel Finnie wrote:

> Is there such a thing?

Rails' ActiveSupport provides them as cattr\_\* and mattr\_\*. For that matter, they're not that hard to write. For that matter, what Eric said.

Devin  
What, me matter?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [10 December 2006 13:06 UTC](https://rubytalk.org/t/attr-reader-etc-for-class-variables/33612/4 "2006-12-10T13:06:23Z")

</div>

Hi --

> Daniel Finnie wrote:
> 
> > Is there such a thing?
> 
> Rails' ActiveSupport provides them as cattr\_\* and mattr\_\*. For that matter, they're not that hard to write. For that matter, what Eric said.

I'd add that if you write them (and I agree with Eric that instance  
variables are much better for this; indeed, something is pretty much  
always better than class variables for everything), it's best to name  
them something else. "attr" names suggest an attribute of an object,  
but since class variables are not uniquely associated with any one  
object, they're not a good fit for recording an attribute.

David

> **···**
>
> On Sun, 10 Dec 2006, Devin Mullins wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
