Why no class_variable_set?

  • Thread starter Michael Schuerig
  • Start date
M

Michael Schuerig

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
 
E

Eric Mahurin

--- Michael Schuerig said:
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/weekend.html
 
M

Michael Schuerig

Eric said:
--- Michael Schuerig said:
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
 
Y

Yukihiro Matsumoto

Hi,

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

matz.
 
M

Michael Schuerig

Yukihiro said:
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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Why no class_variable_set?"

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

Ok, I will.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top