Forum: Ruby double colons in class def

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.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-03-01 00:00
(Received via mailing list)
In this phrase:

class IterString < ::String
  # whatever
end

what do the double-colons do? Thx - m.
Be30361bb0b0c495e3077db43ad84b56?d=identicon&s=25 Aaron Patterson (Guest)
on 2009-03-01 01:45
(Received via mailing list)
On Sun, Mar 01, 2009 at 07:54:03AM +0900, matt neuburg wrote:
> In this phrase:
>
> class IterString < ::String
>   # whatever
> end
>
> what do the double-colons do? Thx - m.

It's explicitly telling ruby where to find the "String" constant.

For example:

  module Foo
    class String
      def hello
        "world"
      end
    end

    class Bar < String
    end

    class Baz < ::String
    end
  end

  puts Foo::Bar.new.hello
  puts Foo::Baz.new.hello # => (NoMethodError)

The double colons make sure that ruby looks at the top level for the
constant.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-03-01 02:17
(Received via mailing list)
Aaron Patterson <aaron@tenderlovemaking.com> wrote:

> The double colons make sure that ruby looks at the top level for the constant.
Right, I see, I was testing it in a place where String and ::String were
the same; that's why I couldn't see a difference. :) Thx - m.
This topic is locked and can not be replied to.