在类名之前,我在Rails中见过很多双冒号的用法.
例如:
require ::File.expand_path('../config/environment', __FILE__)
我知道Module::Class::Constant
是什么意思,但是::Class
?
在类名之前,我在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