[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why no class_variable_set?

Michael Schuerig

6/7/2005 12:14:00 AM


I'm looking for a clean way to reflectively set the value of a class
variable. Currently I'm using something along the lines of

class_eval("@@var = #{value.inspect}")

which I don't like much. There is no class_variable_set, but, so I
thought, maybe instance variables of the class object are treated as
class variables, thus I tried

klass.instance_variable_set(:@@var, value)

`@@var' is not allowed as an instance variable name

So, this doesn't work. Then there's

class_eval <<END
def self.var=(value)
@@var = value
end
END
private_class_method :var=
self.var = value

Is this the way to go?

Michael

--
Michael Schuerig Most people would rather die than think.
mailto:michael@schuerig.de In fact, they do.
http://www.schuerig.d... --Bertrand Russell

5 Answers

Eric Mahurin

6/7/2005 1:26:00 AM

0

--- Michael Schuerig <michael@schuerig.de> wrote:
> I'm looking for a clean way to reflectively set the value of
> a class
> variable. Currently I'm using something along the lines of
>
> class_eval("@@var = #{value.inspect}")
>
> which I don't like much. There is no class_variable_set, but,
> so I
> thought, maybe instance variables of the class object are
> treated as
> class variables, thus I tried
>
> klass.instance_variable_set(:@@var, value)
>
> `@@var' is not allowed as an instance variable name
>
> So, this doesn't work. Then there's
>
> class_eval <<END
> def self.var=(value)
> @@var = value
> end
> END
> private_class_method :var=
> self.var = value
>
> Is this the way to go?
>
> Michael

Instead of using a class variable, you could use an instance
variable of the class:

class XYZ
def self.var=(value)
@var = value
end
def self.var
@var
end
end

XYZ.var and XYZ.var= will deal with an instance variable of the
class object XYZ which is like a class variable (but not
directly accessible from the instances of XYZ). I don't see
much of a reason to have class variables in the language since
you can do this.




__________________________________
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/we...



Michael Schuerig

6/7/2005 1:44:00 AM

0

Eric Mahurin wrote:

> --- Michael Schuerig <michael@schuerig.de> wrote:
>> I'm looking for a clean way to reflectively set the value of
>> a class
>> variable. Currently I'm using something along the lines of
[snip]
>> class_eval <<END
>> def self.var=(value)
>> @@var = value
>> end
>> END
>> private_class_method :var=
>> self.var = value
>>
>> Is this the way to go?


> Instead of using a class variable, you could use an instance
> variable of the class:
>
> class XYZ
> def self.var=(value)
> @var = value
> end
> def self.var
> @var
> end
> end

That's what I would probably do in the general case. As I'm using Rails
in this specific case, I've settled on a variant of my example above.

cattr_accessor :var
private_class_method :var=
self.var = value

Michael

--
Michael Schuerig You can twist perceptions
mailto:michael@schuerig.de Reality won't budge
http://www.schuerig.d... --Rush, Show Don't Tell

Yukihiro Matsumoto

6/7/2005 5:19:00 AM

0

Hi,

1.9 has class_variable_set(). Should I back port it to the 1.8?

matz.


Michael Schuerig

6/7/2005 7:54:00 AM

0

Yukihiro Matsumoto wrote:

> 1.9 has class_variable_set(). Should I back port it to the 1.8?

Hi matz,

if you don't mind, then yes, please, back port it.

Michael

--
Michael Schuerig Life is just as deadly
mailto:michael@schuerig.de As it looks
http://www.schuerig.d... --Richard Thompson, Sibella

Yukihiro Matsumoto

6/7/2005 8:45:00 AM

0

Hi,

In message "Re: Why no class_variable_set?"
on Tue, 7 Jun 2005 16:55:32 +0900, Michael Schuerig <michael@schuerig.de> writes:

|if you don't mind, then yes, please, back port it.

Ok, I will.

matz.