End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Module#class_variable_set
obj.class_variable_set(symbol, obj) → obj
obj.class_variable_set(string, obj) → obj
object.c
View on GitHub ↗
static VALUE
rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
{
ID id = id_for_var(obj, iv, class);
if (!id) id = rb_intern_str(iv);
rb_cvar_set(obj, id, val);
return val;
}
Sets the class variable named by symbol to the given object. If the class variable name is passed as a string, that string is converted to a symbol.
Example
class Fred @@foo = 99 def foo @@foo end end Fred.class_variable_set(:@@foo, 101) #=> 101 Fred.new.foo #=> 101
See also
#class_variables
#constants
#class_variable_get
#const_defined?
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1