views:

285

answers:

5

I'd probably be able to answer this for myself if "::" wasn't so hard to Google. Didn't see anything on SO so thought I'd try my luck.

What is this double-colon :: all about? I see it everywhere in Rails:

class User < ActiveRecord::Base

or…

ActionController::Routing::Routes.draw do |map|

I found a definition from this guy:

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.

but that just leads to more questions. What good is scope (private, protected) if you can just use :: to expose anything?

+1  A: 

It is all about preventing definitions from clashing with other code linked in to your project. It means you can keep things separate.

For example you can have one method called "run" in your code and you will still be able to call your method rather than the "run" method that has been defined in some other library that you have linked in.

Mongus Pong
+12  A: 

:: 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 private methods defined on a class, since you access those with a different syntax (the dot .).

mipadi
would it be like `SomeModule.InnerModle.MyClass.CONSTANT` in "other" languages ?
OscarRyz
In C#, for instance, yes. On the other hand C++ (and Ruby) use `::` for namespace resolution such as `std::cout << "Hello World!";`
Jerry Fernholz
Relevant note: If you want to go back to the top-level namespace, do this: `::SomeModule`
Benjamin Oakes
+1  A: 

:: Lets you access a constant, module, or class defined inside another class or module. It is used to provide namespaces so that method and class names don't conflict with other classes by different authors.

When you see ActiveRecord::Base in Rails it means that Rails has something like

module ActiveRecord
  class Base
  end
end

i.e. a class called Base inside a module ActiveRecord which is then referenced as ActiveRecord::Base (you can find this in the Rails source in activerecord-n.n.n/lib/active_record/base.rb)

A common use of :: is to access constants defined in modules e.g.

module Math
  PI = 3.141 # ...
end

puts Math::PI

The :: operator does not allow you to bypass visibility of methods marked private or protected.

mikej
A: 

What good is scope (private, protected) if you can just use :: to expose anything?

In Ruby, everything is exposed and everything can be modified from anywhere else.

If you're worried about the fact that classes can be changed from outside the "class definition", then Ruby probably isn't for you.

On the other hand, if you're frustrated by Java's classes being locked down, then Ruby is probably what you're looking for.

Justice
I've heard some rubyists say that instance variables aren't exposed, that even `attr_accessor` merely makes methods that modify the variable. (Then again there's `instance_eval`)
Andrew Grimm
Correct, there's `instance_eval`. But there's also `instance_variable_get` and `instance_variable_set`. Ruby is just too dynamic for constraints.
Justice
A: 

No, it is not to access every method, it is a "resolution" operator, that is, you use it to resolve the scope (or location you can say) of a constant/static symbol.

For example in the first of your line, Rails use it to find the Base class inside the ActiveRecord.Module, in your second one it is used to locate the class method (static) of the Routes class, etc, etc.

It is not used to expose anything, its used to "locate" stuff around your scopes.

http://en.wikipedia.org/wiki/Scope_resolution_operator

Francisco Soto
by "(static)" do you mean "(draw)"?!?
Meltemi