在Ruby中重新定义一些常量(例如,FOO = 'bar'
)会生成警告already initialized constant
。Ruby - 禁止常量重定义
我试图写一个排序ReallyConstants
模块,其中该代码应该有这样的行为:
ReallyConstants.define_constant(:FOO, 'bar') #=> sets the constant ReallyConstants::FOO to 'bar'
ReallyConstants::FOO #=> 'bar'
ReallyConstants::FOO = 'foobar' #=> this should raise an Exception
就是不断重新定义应该产生异常。
这可能吗?
[在Ruby中重新分配常量时抛出异常?](http://stackoverflow.com/questions/3023617/throw-exception-when-re-assigning-a-constant-in-ruby) –