[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attr_reader, etc for class variables

Daniel Finnie

12/10/2006 5:25:00 AM

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

3 Answers

Eric Hodel

12/10/2006 7:24:00 AM

0

On Dec 9, 2006, at 21:25 , Daniel Finnie wrote:

> Is there such a thing?

No.

> 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 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 Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Devin Mullins

12/10/2006 7:32:00 AM

0

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?

dblack

12/10/2006 1:06:00 PM

0