# Attr\_reader, etc for class variables

**URL:** <https://www.ruby-forum.com/t/attr-reader-etc-for-class-variables/78897>\
**Category:** Ruby\
**Created:** [December 10, 2006, 6:26am UTC](https://www.ruby-forum.com/t/attr-reader-etc-for-class-variables/78897 "2006-12-10T06:26:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 6:26am UTC](https://www.ruby-forum.com/t/attr-reader-etc-for-class-variables/78897/1 "2006-12-10T06:26:44Z")

</div>

Is there such a thing?

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

How can I make an attr\_writer for it besides doing  
class Klass  
def var=(other)  
@@var = other  
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:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 8:24am UTC](https://www.ruby-forum.com/t/attr-reader-etc-for-class-variables/78897/2 "2006-12-10T08:24:48Z")

</div>

On Dec 9, 2006, at 21:25 , Daniel F. wrote:

> Is there such a thing?

No.

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

class Klass  
@var = 0

class \<\< self  
attr\_accessor :var  
end

def some\_method  
self.class.var  
end  
end

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

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 8:36am UTC](https://www.ruby-forum.com/t/attr-reader-etc-for-class-variables/78897/3 "2006-12-10T08:36:48Z")

</div>

Daniel F. 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:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/attr-reader-etc-for-class-variables/78897/4 "2006-12-10T14:07:04Z")

</div>

Hi –

On Sun, 10 Dec 2006, Devin M. wrote:

> Daniel F. 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
