Forum: Ruby :: without a prefix (global?)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-12-19 06:47
I've found some cases where things don't work correctly unless they're
prefixed with a :: .  I'm really confused - isn't :: used to specify the
module (like Graphics::Circle)?

What is ::Circle? No module?!
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2005-12-19 07:00
(Received via mailing list)
On Dec 18, 2005, at 9:47 PM, List Recv wrote:

> I've found some cases where things don't work correctly unless they're
> prefixed with a :: .  I'm really confused - isn't :: used to
> specify the
> module (like Graphics::Circle)?
>
> What is ::Circle? No module?!

The constant Circle at toplevel.

$ irb
irb(main):001:0> A = 1
=> 1
irb(main):002:0> module M
irb(main):003:1> A = 2
irb(main):004:1> puts A
irb(main):005:1> puts ::A
irb(main):006:1> end
2
1
=> nil

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-19 14:38
(Received via mailing list)
List Recv wrote:
> I've found some cases where things don't work correctly unless they're
> prefixed with a :: .  I'm really confused - isn't :: used to specify the
> module (like Graphics::Circle)?
>
> What is ::Circle? No module?!

`::' will take you to the top level.

   Bar = "::Bar"

   module Foo
     Bar = "Foo::Bar"

     def self.bur
       puts Bar    => "Foo::Bar"
       puts ::Bar  => "::Bar"
     end
   end



Cheers,
Daniel
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-12-20 04:15
Thanks.

Is there any case where you will use :: when you have not defined a
class with the same name in the local module?

I was working on a project where I could not access a certain class
within a module without prefixing it with ::, yet I had never (to my
knowledge) redefined it.

(FYI, in a Rails ActiveController, you can catch
::ActiveController::NameOfExceptionClass, but you need the :: ....
weird!)


Eric Hodel wrote:
> On Dec 18, 2005, at 9:47 PM, List Recv wrote:
>
>> I've found some cases where things don't work correctly unless they're
>> prefixed with a :: .  I'm really confused - isn't :: used to
>> specify the
>> module (like Graphics::Circle)?
>>
>> What is ::Circle? No module?!
>
> The constant Circle at toplevel.
>
> $ irb
> irb(main):001:0> A = 1
> => 1
> irb(main):002:0> module M
> irb(main):003:1> A = 2
> irb(main):004:1> puts A
> irb(main):005:1> puts ::A
> irb(main):006:1> end
> 2
> 1
> => nil
>
> --
> Eric Hodel - drbrain@segment7.net - http://segment7.net
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.robotcoop.com
This topic is locked and can not be replied to.