rails autoload_paths 中的错误? [英] Bug in rails autoload_paths?
问题描述
我在我的代码中遇到了一个奇怪的错误.我有一个 rails 应用程序,lib 中有以下两个文件:
I am encountering a strange bug in my code. I have a rails application with the following two files in the lib:
lib/module_one/module_two/class_one.rb
lib/module_one/module_two/class_one.rb
module ModuleOne
module Moduletwo
class ClassOne
class << self
def test
puts 'Class one'
ClassTwo.test
end
end
end
end
end
和
lib/module_one/module_two/class_two.rb
lib/module_one/module_two/class_two.rb
module ModuleOne
module ModuleTwo
class ClassTwo
def self.test
puts 'Class two'
end
end
end
end
现在我的问题是,当我进入控制台并写入:
Now my problem is, that when I go into the console and write:
ModuleOne::ModuleTwo::ClassOne.test
它抛出以下内容:NameError: uninitialized constant ClassTwo
奇怪的是,这个问题似乎与使用 class << 有关.self
而不是 self.method
.如果我像这样更改 class_one.rb 文件,它会起作用!:
The strange thing is, that the problem seems to be connected to the use of class << self
instead of self.method
. If I change the class_one.rb file like this it works!:
module ModuleOne
module ModuleTwo
class ClassOne
def self.test
puts 'Class one'
ClassTwo.test
end
end
end
end
我正在像这样加载 application.rb 中的文件:
Im loading the files in application.rb like this:
config.autoload_paths += %W(#{config.root}/lib)
这是 Rails 中的错误,还是只是我出错了?
Is this a bug in rails, or is it just me getting something all wrong?
我正在使用 rails 3.1.3 顺便说一句
Im using rails 3.1.3 btw
推荐答案
(仅部分答案,但需要格式化.)
(Only a partial answer, but need formatting.)
这是因为class <<自我
有效.
例如,如果您将其更改为:
For example, if you change it to:
class << self
def test
self::ClassTwo.test
end
end
效果很好.
编辑;太长了,无法进行合理评论.
我正在四处闲逛……从直观的角度来看,这对我来说很有意义,我只是不确定为什么.不知道我是否曾经知道一个真正的原因,或者我只是在编造.
I'm poking around a bit... On an intuitive level it makes sense to me, I'm just not sure why yet. Don't know if I knew a real reason once, or if I'm just making it up.
我不知道为什么 self
似乎指的是这个模块;Programming Ruby 1.9"一书对 class <<
语义的深入研究不够深入.我会发一些推文并参考这个问题,更聪明的人会创建一个真正的答案.
I'm not sure why self
seems to refer to the module, though; the "Programming Ruby 1.9" book doesn't go in to enough depth on the class <<
semantics. I'll tweet something and refer to this question and someone smarter will create a real answer.
这篇关于rails autoload_paths 中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!