【发布时间】:2012-01-06 05:41:04
【问题描述】:
我需要知道包含的 ruby 模块是否可以访问类变量。让我们说:
require 'bar'
class Foo
@i_am_important
Bar.do_stuff
end
Module Bar
def Bar.do_stuff
@i_am_important.stuff...
end
end
有没有办法使上述工作?
编辑:改进的示例, edit2:解决问题
我只是改变了我的方法:Bar 变成了自己的一个类,并在初始化时传递了“i_am_important”。可能不是最好的解决方案,但最终有效。谢谢你的帮助。
【问题讨论】:
-
首先,如果要包含模块,则必须将所有可用于托管类的方法设为实例方法