# Scope resolution operator

**URL:** <https://www.ruby-forum.com/t/scope-resolution-operator/158570>\
**Category:** Ruby\
**Created:** [January 25, 2009, 1:21am UTC](https://www.ruby-forum.com/t/scope-resolution-operator/158570 "2009-01-25T01:21:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 25, 2009, 1:21am UTC](https://www.ruby-forum.com/t/scope-resolution-operator/158570/1 "2009-01-25T01:21:17Z")

</div>

(The code I will demonstrate is Rails-based, but the question is  
Ruby-based.)

I’m familiar with the following:  
ActiveRecord::Base

Basically says, look inside ActiveRecord for a class or module named  
Base.

But what about the following:

::Attachment

Does that basically say, no matter what scope you are in, jump to the  
highest level to look for the class or module named Attachment?

The following code only works when I prefix the Attachment class with ::

has\_one :attachment, :as =\> :attachable, :class\_name =\> ‘::Attachment’

def assign\_attachment=(attachment\_id)  
self.attachment = ::Attachment.find(attachment\_id)  
end

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 25, 2009, 9:15am UTC](https://www.ruby-forum.com/t/scope-resolution-operator/158570/2 "2009-01-25T09:15:11Z")

</div>

Alle domenica 25 gennaio 2009, Daniel W. ha scritto:

> ::Attachment  
> Does that basically say, no matter what scope you are in, jump to the  
> highest level to look for the class or module named Attachment?

According to “The Ruby P.ming Language”, ::Something is a shortcut  
for  
Object::Something. The idea is that ::Something should look up a  
constant  
called Something in the global scope, but since ruby doesn’t truly have  
a  
global scope, it looks it up in the Object class, which is the nearest  
thing  
in ruby to a global scope.

> The following code only works when I prefix the Attachment class with ::
> 
> has\_one :attachment, :as =\> :attachable, :class\_name =\> ‘::Attachment’
> 
> def assign\_attachment=(attachment\_id)  
> self.attachment = ::Attachment.find(attachment\_id)  
> end

Not knowing rails, I can’t say much on this. However, if it works with  
::Attachment and doesn’t work with Attachment, the only thing I can  
think of  
is that you have two constants called Attachment: one at global level,  
which  
is the one you need here, and another at a more local level which is the  
one  
you get without the ::

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 25, 2009, 9:54am UTC](https://www.ruby-forum.com/t/scope-resolution-operator/158570/3 "2009-01-25T09:54:21Z")

</div>

Stefano C. wrote:

> Not knowing rails, I can’t say much on this. However, if it works with  
> ::Attachment and doesn’t work with Attachment, the only thing I can  
> think of  
> is that you have two constants called Attachment: one at global level,  
> which  
> is the one you need here, and another at a more local level which is the  
> one  
> you get without the ::

You may not be familiar with Rails but you certainly know Ruby. That is  
exactly the case. The Paperclip module defines an Attachment class,  
methods of which I am using in my own Attachment class. Talk about a  
naming conflict. Grr…

Thanks for the reply Stefano, it’s helped a lot.

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 27, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/scope-resolution-operator/158570/4 "2009-01-27T14:55:36Z")

</div>

2009/1/25 Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > But what about the following:  
> > ::Attachment  
> > Does that basically say, no matter what scope you are in, jump to the  
> > highest level to look for the class or module named Attachment?
> 
> According to “The Ruby P.ming Language”, ::Something is a shortcut for  
> Object::Something. The idea is that ::Something should look up a constant  
> called Something in the global scope, but since ruby doesn’t truly have a  
> global scope, it looks it up in the Object class, which is the nearest thing  
> in ruby to a global scope.

As an illustration:

14:52:57 ~$ ruby -e ‘puts Object.constants.sort’  
ARGF  
ARGV  
ArgumentError  
Array  
Bignum  
Binding  
Class  
Comparable  
Continuation  
Data  
Dir  
ENV  
EOFError  
Enumerable  
Errno  
Exception  
FALSE  
FalseClass  
File  
FileTest  
Fixnum  
Float  
FloatDomainError  
GC  
Hash  
IO  
IOError  
IndexError  
Integer  
Interrupt  
Kernel  
LoadError  
LocalJumpError  
Marshal  
MatchData  
MatchingData  
Math  
Method  
Module  
NIL  
NameError  
NilClass  
NoMemoryError  
NoMethodError  
NotImplementedError  
Numeric  
Object  
ObjectSpace  
…

You see all the globally scoped classes (later on there will be String  
etc.). Notice also the nice element of self referentiality. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 28, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/scope-resolution-operator/158570/5 "2009-01-28T20:07:54Z")

</div>

- Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (2009-01-25) schrieb:

> According to “The Ruby P.ming Language”, ::Something is a shortcut for  
> Object::Something. The idea is that ::Something should look up a constant  
> called Something in the global scope, but since ruby doesn’t truly have a  
> global scope, it looks it up in the Object class, which is the nearest thing  
> in ruby to a global scope.

,----  
| $ cat /tmp/scope.rb  
| Something = “blubb”  
|  
| module Bla  
| class Object  
| Something = “crazy”  
| end  
|  
| puts ::Something  
| puts Object::Something  
| end  
| $ ruby /tmp/scope.rb  
| blubb  
| crazy  
| $  
`----

mfg, simon … l
