【发布时间】:2013-12-05 17:06:30
【问题描述】:
在 Ruby 2.0 下,从模块中的类访问模块方法的正确方法是什么?
例如,如果我有
module Foo
class Foo
def do_something
Foo::module_method
end
end
def self.module_method
puts 'How do I call this?'
end
end
我明白了,
./so-module.rb:7:in
do_something': undefined methodmodule_method' for Foo::Foo:Class (NoMethodError) from ./so-module.rb:16:in `'
定义模块方法以便我可以从类Foo 访问它的正确方法是什么?
【问题讨论】:
-
你确定这是你得到的错误吗?我得到“NoMethodError: undefined method `module_method' for Foo:Module”。
-
@AndrewMarshall,你说得对,从我的实际代码到将其简化为示例的翻译过程中丢失了一些东西。让我看看我能不能弄明白。
-
啊,好吧,我也遇到了类名与模块名相同的问题,这导致了奇怪的事情发生,所以我已经更正了。
-
您能否更新您的问题以反映这些变化?
-
我实际上并没有得到完全相同的错误,即使更改了脚本,但我会更新它以更准确地反映我原来的问题