Method: Module#const_set
- Defined in:
- object.c
#const_set(sym, obj) ⇒ Object #const_set(str, obj) ⇒ Object
Sets the named constant to the given object, returning that object. Creates a new constant if no constant with the given name previously existed.
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”.
Object.const_set(‘foobar’, 42) #=> NameError: wrong constant name foobar
2535 2536 2537 2538 2539 2540 2541 2542 2543 |
# File 'object.c', line 2535
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;
}
|