Flowdock
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
Show source
Register or log in to add new notes.
August 13, 2009
0 thanks

no overwrite

const_set does not overwrite, it only create new ones

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"