为什么重新打开嵌套模块会根据使用的语法给出不同的结果?例如,这工作正常:
module A
module E
end
end
module A
module E
def E.e
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是这个:
module A
module E
end
end
module A::E
def E.e
end
end
Run Code Online (Sandbox Code Playgroud)
给出错误:
reopen.rb:6:in `<module:E>': uninitialized constant A::E::E (NameError)
from reopen.rb:5:in `<main>'
Run Code Online (Sandbox Code Playgroud)
(在有人指出这一点之前,解决方法是self在定义Ee时使用而不是模块名称,但这不是这篇文章的重点.)