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
2583 2584 2585 2586 2587 2588 2589 2590 2591 |
# File 'object.c', line 2583 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; } |