Forum: Ruby Scope resolution operator

42292bf8a0acbc98862d9306b501275d?d=identicon&s=25 Daniel Waite (rabbitcreative)
on 2009-01-25 01:21
(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
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-01-25 09:15
(Received via mailing list)
Alle domenica 25 gennaio 2009, Daniel Waite 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 Programming 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
42292bf8a0acbc98862d9306b501275d?d=identicon&s=25 Daniel Waite (rabbitcreative)
on 2009-01-25 09:54
Stefano Crocco 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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-27 14:55
(Received via mailing list)
2009/1/25 Stefano Crocco <stefano.crocco@alice.it>:
>> 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 Programming 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
1d53b088a989e069b94597c282eebbbc?d=identicon&s=25 Simon Krahnke (Guest)
on 2009-01-28 20:07
(Received via mailing list)
* Stefano Crocco <stefano.crocco@alice.it> (2009-01-25) schrieb:

> According to "The Ruby Programming 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.