开发者

Double colons before class names in Ruby?

开发者 https://www.devze.com 2023-02-07 15:55 出处:网络
I have seen a lot usage of double colons in Rails before cla开发者_JAVA技巧ss names. For example:

I have seen a lot usage of double colons in Rails before cla开发者_JAVA技巧ss names.

For example:

require ::File.expand_path('../config/environment',  __FILE__)

I know what Module::Class::Constant means, but ::Class ?


It means that you're referring to the constant File from the toplevel namespace. This makes sense in situations like this:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消