Attr_reader, etc for class variables

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 nameKlass.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 nameself.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

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. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

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?

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