omt*_*mtr 37 ruby ruby-on-rails
在类名之前,我已经在Rails中看到了很多双冒号的用法.
例如:
require ::File.expand_path('../config/environment', __FILE__)
Run Code Online (Sandbox Code Playgroud)
我知道是什么Module::Class::Constant意思,但是::Class?
sep*_*p2k 69
这意味着你File从toplevel命名空间引用常量.这在以下情况下有意义:
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
Run Code Online (Sandbox Code Playgroud)