我在 Rails 中看到很多在类名之前使用双冒号的情况。
例如:
require ::File.expand_path('../config/environment', __FILE__)
我知道什么Module::Class::Constant
意思是,但是::Class
?
这意味着你指的是常数File
来自顶级命名空间。这在以下情况下是有意义的:
class MyClass #1
end
module MyNameSpace
class MyClass #2
end
def foo # Creates an instance of MyClass #1
::MyClass.new # If I left out the ::, it would refer to
# MyNameSpace::MyClass instead.
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)