【发布时间】:2011-09-09 09:58:57
【问题描述】:
背景:
- ruby thinks I'm referencing a top-level constant even when I specify the full namespace
- How do I refer to a submodule's "full path" in ruby?
这里的问题,提炼成一个最小的例子:
# bar.rb
class Bar
end
# foo/bar.rb
module Foo::Bar
end
# foo.rb
class Foo
include Foo::Bar
end
# runner.rb
require 'bar'
require 'foo'
➔ ruby runner.rb
./foo.rb:2:警告:Foo::Bar 引用的顶级常量 Bar
./foo.rb:2:in `include': 错误的参数类型 Class (expected Module) (TypeError)
来自 ./foo.rb:2
来自 runner.rb:2:in `require'
来自 runner.rb:2
【问题讨论】:
-
我只包括 gem,然后它需要库并执行 rails gems 可能执行的任何其他钩子。所以你是说,也许我可以进入供应商提供的 gem 并明确地制作所有内容
::Foo?我认为问题出在反面……当我包含User::Foo时,ruby 首先在顶层搜索Foo……请参阅我在上面更新中提到的线程。
标签: ruby class namespaces module