Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does :: (double colon) mean in Ruby? [duplicate]

What does :: mean in Ruby? E.g. Foo::Bar.

like image 316
eric Avatar asked Feb 16 '10 22:02

eric


People also ask

What does the :: mean in Ruby?

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.

What does double colon mean in Ruby?

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.

What Does a colon mean in Ruby?

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.

What does double colon mean in programming?

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)


3 Answers

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 a receiver::Thing method call is actually an attempt to access a constant called Thing in the receiver unless the method invocation has a parameter list between parentheses.

like image 127
Mark Byers Avatar answered Oct 22 '22 17:10

Mark Byers


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.

like image 9
Achilles Avatar answered Oct 22 '22 16:10

Achilles


It accesses constants in a given class or module. E.g. ActiveRecord::Base is the constant Base defined in the module ActiveRecord.

like image 3
sepp2k Avatar answered Oct 22 '22 17:10

sepp2k