What does ::
mean in Ruby? E.g. Foo::Bar
.
The use of :: on the class name means that it is an absolute, top-level class; it will use the top-level class even if there is also a TwelveDaysSong class defined in whatever the current module is.
The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module.
Ruby symbols are created by placing a colon (:) before a word. You can think of it as an immutable string. A symbol is an instance of Symbol class, and for any given name of symbol there is only one Symbol object. :apple.object_id.
Use the double colon operator (::) to qualify a C++ member function, a top level function, or a variable with global scope with: An overloaded name (same name used with different argument types) An ambiguous name (same name used in different classes)
From the Pickaxe:
When a receiver is explicitly specified in a method invocation, it may be separated from the method name using either a period (
.
) or two colons (::
). The only difference between these two forms occurs if the method name starts with an uppercase letter. In this case, Ruby will assume that areceiver::Thing
method call is actually an attempt to access a constant calledThing
in the receiver unless the method invocation has a parameter list between parentheses.
It's called a scope resolution operator. Basically a fancy way of referencing a class within a namespace. ActiveRecord is the namespace and Base is the class.
It accesses constants in a given class or module. E.g. ActiveRecord::Base
is the constant Base
defined in the module ActiveRecord
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With