Double colons before class names in Ruby?
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
What is Ruby's double-colon `::`?
::
is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup:
module SomeModule
module InnerModule
class MyClass
CONSTANT = 4
end
end
end
You could access CONSTANT
from outside the module as SomeModule::InnerModule::MyClass::CONSTANT
.
It doesn't affect instance methods defined on a class, since you access those with a different syntax (the dot .
).
Relevant note: If you want to go back to the top-level namespace, do this: ::SomeModule – Benjamin Oakes
Idiomatic use of double-colon (double-column, or ::) syntax for Ruby methods
Would it be non-idiomatic for me to avoid ever using the
::
operator for qualifying the names of anything but classes, modules, and constants and, instead, consistently to use only dot syntax for all methods (class methods, module methods, and instance methods)?
No, it would in fact be idiomatic. Nobody ever uses ::
to call methods, ever. Period.
The only time ::
is used in conjunction with methods is when talking about methods. In that case, Foo#bar
is used to talk about instance methods (i.e. a method bar
that can be called on instances of Foo
like foo.bar
) and Foo::bar
is used to talk about singleton methods (i.e. a method bar
that can be called on Foo
itself like Foo.bar
). However, we only use ::
for talking about methods, never for calling them.
Some notes:
- Technically speaking, there are no class methods or module methods in Ruby. Every object can have methods defined only for itself (so-called singleton methods), and since classes and modules are also objects, they can also have singleton methods. So, we will sometimes talk about "class methods" or "module functions", but in reality, those are just singleton methods.
- Well … actually, I lied. Sorry. Singleton methods don't exist either. They are really just normal instance methods of the singleton class of the object. But saying "instance method of the singleton class of the object" is a mouthful, so we just say "singleton method of the object", and likewise instead of "instance method of the class object's singleton class", we say just "class method". However, we only say this in the knowledge that those things actually don't really exist, and especially when talking to newbies, I prefer to use the long form instead of the short-form jargon.
::
is used to dereference constants inside modules. The fact that those constants often point to modules or classes is not significant. Here is an example of resolving a constant which doesn't point to a module or class inside a module that isn't referenced by constant:module Foo; BAR = 42 end
foo = Foo
foo::BAR # => 42
# ^ ^------------ not a module
# |
# +---------------- not a constant
Ruby's double colon (::) operator usage differences
Constants in Ruby are nested like files and directories in filesystem. So, constants are uniquely identified by their paths.
To draw an analogy with the file system:
::Rails::Engine #is an absolute path to the constant.
# like /Rails/Engine in FS.
Rails::Engine #is a path relative to the current tree level.
# like ./Rails/Engine in FS.
Here is the illustration of possible error:
module Foo
# We may not know about this in real big apps
module Rails
class Engine
end
end
class Engine1 < Rails::Engine
end
class Engine2 < ::Rails::Engine
end
end
Foo::Engine1.superclass
=> Foo::Rails::Engine # not what we want
Foo::Engine2.superclass
=> Rails::Engine # correct
difference between dot(.) and double colon (::) in accessing class method
The double colon ::
namespace operator can also be used as a message sending operator. In other words,
foo.bar
can also be written as
foo::bar
Except when not.
In particular, .
is always a message send. ::
is usually a namespace lookup, except when it cannot possibly be. That means, for example, you cannot call a message that starts with an uppercase character, unless you also pass an argument list.
foo = Class.new do
def BAR; :method end
BAR = :constant
end
foo.BAR #=> :method
foo::BAR #=> :constant
foo::BAR() #=> :method
The fact that ::
can also be used for message sends is a historical curiosity, and is banned in most style guides except for "class factories", i.e. methods that return classes. Imagine a web framework that is set up like this:
module Controller
def self.R(path)
Class.new(AbstractController) do
# a bunch of methods for dealing with routing to `path`
end
end
end
class IndexController < Controller::R '/index.html'
def get
render 'Welcome'
end
end
In this case, in some style guides, it would be acceptable to write Controller::R
because even though R
is a method, it returns a class, so it kind-of acts like one.
But this is a special case for certain DSLs and is only allowed in certain style guides. Most style guides disallow ::
for message sends, because it is redundant with .
, because it already has a another different meaning (namespace resolution), and because it doesn't behave like .
in all cases.
What is the difference in accessing class method using dot(.) and a double colon (::) operator then?
On the one hand, you can say, there is no difference because when used as the message sending operator, they both do the exact same thing.
On the other hand, there is a difference in syntax, namely that foo::BAR
isn't a message send, it is a namespace lookup which is completely different. from foo.BAR
, which is a message send.
Purpose of :: before include module in ruby
Calling ::Module1::Module2
would be referencing the top-level namespace, rather than the relative namespace of where the statement is being executed.
This is useful in scenarios where you're trying to reference a top-level module within another module, like so:
class YourClass
end
module YourModule
class YourClass
end
def self.new_outer_class
::YourClass.new
end
def self.new_inner_class
YourClass.new
end
end
With the above setup, calling #new_outer_class
would actually instantiate the instance of YourClass
class defined outside of YourModule
, whereas calling #new_inner_class
would instantiate YourModule::YourClass
since it's relative to and called within YourModule
.
Hope that helps!
Related Topics
How to Generate a Random String in Ruby
How to Test If a String Is Basically an Integer in Quotes Using Ruby
Combining Implicit Wait and Explicit Wait Together Results in Unexpected Wait Times
How to Parse Json With Ruby on Rails
What Is the Best Method of Handling Currency/Money
How to Update Ruby Gems from Behind a Proxy (Isa-Ntlm)
Difference Between Class Variables and Class Instance Variables
Ruby: String Comparison Issues
Difference Between \A \Z and ^ $ in Ruby Regular Expressions
How to Temporarily Redirect Stderr in Ruby
Paginating an Array in Ruby With Will_Paginate
How to Remove Emoji from String
Cannot Load Such File - Script/Rails: Getting This Error While Remote Debugging Through Rubymine