On Wed, Oct 17, 2007 at 03:10:07AM +0900, Gavin Kistner wrote:
> From: John Lam (DLR) [mailto:jflam / microsoft.com] 
> > 2. Bad side effect during global constant assignment:

At first glance I agree it looks like a bug but it's consistant behavior with
the way anonymous classes resolve their name. 

> I think the following shows the bug slightly more clearly:
> 
> class E
>   p name           #=> "E"
>   class << self
>     p name         #=> ""
>     Foo = self
>     p name         #=> ""
>     ::Foo = self
>     p name         #=> "Foo"

>   end
>   p name           #=> "E"
> end

The meta class of E is an anonymous class. So it does not have a name.

irb(main):005:0> class E
irb(main):006:1> end
=> nil
irb(main):007:0> class << E
irb(main):008:1> p self
irb(main):009:1> end
#<Class:E>

Assigning an anonymous class to a constant implicitly gives that class the
name of the constant. So since the meta class of E (which is anonymous) is
assigned to the outer Foo constant, it's given that name.

c = Class.new
=> #<Class:0x6c2d0c>
irb(main):002:0> c.name
=> ""
irb(main):003:0> Bar = c
=> Bar
irb(main):004:0> c.name
=> "Bar"

marcel
-- 
Marcel Molina Jr. <marcel / vernix.org>