【发布时间】:2017-07-27 20:52:48
【问题描述】:
我有一个模块如下
-MyClass 类 lib/a/b/myclass.rb 的路径
module A
module B
class MyClass
puts 'inside myclass'
end
end
end
现在我想从根目录中的文件自动加载上述类。 文件名:dostuff
def main
autoload A::B:MyClass,'a/b/myclass.rb' #path is correct , getting error here
c = A::B:MyClass.new
end
main
出现错误:uninitialized constant A::B::MyClass (NameError)
如果我按如下方式使用 require 并删除自动加载代码,一切正常。
require 'a/b/myclass'
【问题讨论】:
-
我的班级还是我的班级?
-
在您的 LOAD_PATH 中是否为
lib? -
请忽略错别字。如果我使用 require 'a/b/myclass' 加载模块,一切正常。但我想出于其他原因使用自动加载。 lib 在我的加载路径中。
-
@SergioTulentsev 你能帮我解决这个问题吗?