From: Eric Mahurin Date: 2005-06-07T10:25:32+09:00 Subject: Re: Why no class_variable_set? --- Michael Schuerig 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 < 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