21

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

Andrew Marshall
  • 92,562
  • 20
  • 215
  • 211
eric
  • 211
  • 2
  • 3

3 Answers3

22

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.

Andrew Marshall
  • 92,562
  • 20
  • 215
  • 211
Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
9

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.

Achilles
  • 10,915
  • 8
  • 61
  • 112
3

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

sepp2k
  • 353,842
  • 52
  • 662
  • 667