Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Module#class_variable_set

[T] (::interned name, T value) -> T
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