Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Module#const_set

mod.const_set(sym, obj) → obj
mod.const_set(str, obj) → obj

Sets the named constant to the given object, returning that object. Creates a new constant if no constant with the given name previously existed.

Example
Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0)   #=> 3.14285714285714
Math::HIGH_SCHOOL_PI - Math::PI              #=> 0.00126448926734968

If sym or str is not a valid constant name a NameError will be raised with a warning “wrong constant name”.

Example
Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar