11

I have some inherited code that I am modifying. However, I am seeing something strange(to me).

I see some code like this:

::User.find_by_email(params[:user][:email]).update_attributes(:mag => 1)

I have never seen something like this(I am new to Ruby on Rails). What does this do and why doesn't my User.find_by_email(params[:user][:email]).update_attributes(:mag => 1) work? The error says something about the User constant.

I am using Rails 2.3.5 if that helps.

Teej
  • 12,764
  • 9
  • 72
  • 93

5 Answers5

23

:: is a scope resolution operator, it effectively means "in the namespace", so ActiveRecord::Base means "Base, in the namespace of ActiveRecord"

A constant being resolved outside of any namespace means exactly what it sounds like - a constant not in any namespace at all.

It's used in places where code may be ambiguous without it:

module Document
  class Table # Represents a data table

    def setup
      Table # Refers to the Document::Table class
      ::Table # Refers to the furniture class
    end

  end
end

class Table # Represents furniture
end
Gareth
  • 133,157
  • 36
  • 148
  • 157
5

It makes sure to load the User model in the global namespace.

Imagine you have a global User model and another User model in your current module (Foo::User). By Calling ::User you make sure to get the global one.

Lennart Koopmann
  • 826
  • 1
  • 8
  • 14
3

Ruby uses (among other things) lexical scoping to find constant names. For example, if you have this code:

module Foo
  class Bar
  end

  def self.get_bar
    Bar.new
  end
end

class Bar
end

The Foo.get_bar returns an instance of Foo::Bar. But if we put :: in front of a constant name, it forces Ruby to only look in the top level for the constant. So ::Bar always refers the top-level Bar class.

You will run into situations in Ruby where the way your code is being run will force you to use these 'absolute' constant references to get to the class you want.

jcoglan
  • 2,108
  • 1
  • 15
  • 12
2

You might find a lead here: What is Ruby's double-colon `::`?

Community
  • 1
  • 1
Kevin A. Naudé
  • 3,992
  • 19
  • 20
  • 1
    That explains what the :: does, but not what a :: with no identifier in front does. :) – Lennart Koopmann Jan 28 '11 at 14:22
  • 1
    True enough. I don't use Ruby, but the '::' operator looks to be a scope resolution operator. My guess is that '::User' simpy means the global User identifier in the current module. Can someone confirm? – Kevin A. Naudé Jan 28 '11 at 14:26
  • @Lennart: Thanks. I awarded you a +1 for getting your answer in before my clarification. Also, +1 to Gareth for a slightly clearer answer than ours. – Kevin A. Naudé Jan 28 '11 at 14:34
1

The "::" operator is used to access Classes inside modules. That way you can also indirectly access methods. Example:

module Mathematics
    class Adder
       def Adder.add(operand_one, operand_two)
           return operand_one + operand_two
       end
    end
end

You access this way:

puts “2 + 3 = “ + Mathematics::Adder.add(2, 3).to_s
RinoFM
  • 78
  • 1
  • 6