我有,我想在一个命名空间来定义模块的一堆,我把它们放在一个初始化,但我得到一个“错误常量名”例外:错误常量名Object.const_set嵌套模块
# this is a self-contained example
require 'active_support/all'
# ==> true
[:baz, :qux].each do |name|
Object.const_set("FooBar::#{name.to_s.camelize}", Module.new {
define_singleton_method :my_awesome_static_method do |amount|
DoSomething.calculate(amount)
end
})
end
# =NameError: wrong constant name FooBar::Baz
# = from (irb):4:in `const_set'
# = from (irb):4:in `block in irb_binding'
# = from (irb):3:in `each'
# = from (irb):3
# = from /usr/local/var/rbenv/versions/2.0.0-p353/bin/irb:12:in `<main>'
我认为我得到这个,因为初始化FooBar
之前运行的定义,但问题是,如果我在这个初始化定义它,FooBar.constants
是空的,我不知道什么是错的。
我看,我有两件事情错了,'const_set'被发送到'FooBar',不'Object',并且名称不包括冒号。 – tlehman