On 8/24/06, Mauricio Fernandez <mfp / acm.org> wrote:

>
> class X; end
> module Y; class ::X; def foo; "X#foo"; end end end
> X.new.foo                                          # => "X#foo"

Really? Was this under 1.8.x or 1.9, I'd be surprised if it was
either, a gedanken experiment perhaps?

irb(main):001:0> class X
irb(main):002:1> end
=> nil
irb(main):003:0> module Y; class X; def foo; "X#foo"; end; end
irb(main):004:1> end
=> nil
iirb(main):005:0> X.new.foo
NoMethodError: undefined method `foo' for #<X:0xb7d4f560>
        from (irb):6
        from :0
irb(main):006:0> Y::X.new.foo
=> "X#foo"

The original poster's supposition is correct, modules (and classes for
that matter) are namespaces,

irb(main):001:0> module X1
irb(main):002:1>    class Time
irb(main):003:2>    end
irb(main):004:1> end
=> nil
irb(main):005:0> X1::Time == Time
=> false

The Time class inside X1 is different than the Time class!

irb(main):006:0> module X1
irb(main):007:1>    class Time
irb(main):008:2>      def foo
irb(main):009:3>        "foo"
irb(main):010:3>      end
irb(main):011:2>    end
irb(main):012:1> end
=> nil
irb(main):013:0> Time.new.foo
NoMethodError: undefined method `foo' for Thu Aug 24 19:38:52 EDT 2006:Time
        from (irb):13
        from :0
irb(main):014:0> X1::Time.new.foo
=> "foo"

With it's own methods!

And if we just refer to Time inside X1

irb(main):023:0> module X1
irb(main):024:1>     def self.myTime
irb(main):025:2>        Time
irb(main):026:2>     end
irb(main):027:1> end
=> nil
irb(main):028:0> X1.myTime
=> X1::Time

We get X1's Time.

Now that begs the question of how to refer to the 'real' Time class
inside a module. One way is with an "empty namespace."

irb(main):015:0> module X1
irb(main):016:1>    def self.baseTime
irb(main):017:2>        ::Time
irb(main):018:2>    end
irb(main):019:1> end
=> nil
irb(main):020:0> X1.baseTime
=> Time
irb(main):021:0> X1.baseTime == Time
=> true


-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/