Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Module#const_set
[T] (::interned name, T value) -> T
mod.const_set(sym, obj) → obj
mod.const_set(str, obj) → obj
object.c
View on GitHub ↗
static VALUE
rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
{
ID id = id_for_var(mod, name, const);
if (!id) id = rb_intern_str(name);
rb_const_set(mod, id, value);
return value;
}
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
See also
#const_missing
#const_source_location
#const_get
#define_method
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0