method

const_set

v1_8_6_287 - Show latest stable - Class: Module
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

2Notes

no overwrite

brian · Aug 13, 2009

const_set does not overwrite, it only create new ones

overwrite

rafaelss · Nov 9, 2009

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"