Why no class_variable_set?

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.de/michael/ --Bertrand Russell

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.

···

--- 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

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

Hi,

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

              matz.

Eric Mahurin 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 <michael@schuerig.de> wrote:

--
Michael Schuerig You can twist perceptions
mailto:michael@schuerig.de Reality won't budge
Michael Schürig | Sentenced to making sense --Rush, Show Don't Tell

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
Michael Schürig | Sentenced to making sense --Richard Thompson, Sibella

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.