Skip to content

End of Life Ruby 3.1 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

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