60

I have seen a lot usage of double colons in Rails before class names.

For example:

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

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

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
omtr
  • 869
  • 2
  • 7
  • 7

1 Answers1

105

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
sepp2k
  • 363,768
  • 54
  • 674
  • 675