method
const_set
const_set(p1, p2)
public
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
Register or
log in
to add new notes.
brian -
August 13, 2009
0 thanks
rafaelss -
November 9, 2009
0 thanks
overwrite
Replacing old value with new one
>> Module.const_set('MY_CONSTANT', 'value') => "value" >> Module::MY_CONSTANT => "value" >> Module.const_set('MY_CONSTANT', 'new value') (irb):3: warning: already initialized constant MY_CONSTANT => "new value" >> Module::MY_CONSTANT => "new value"
or
>> Kernel.const_set('MY_CONSTANT', 'value') => "value" >> MY_CONSTANT => "value" >> Kernel.const_set('MY_CONSTANT', 'new value') (irb):3: warning: already initialized constant MY_CONSTANT => "new value" >> MY_CONSTANT => "new value"