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 ::
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 ::
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…
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.
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.