"And class definition checks if the class is already defined, to reopen it"

Sure, and that's my point. It checks if it is already defined. If it is not=
, which is the case since load_c doesn't define C directly in F class, it c=
reates a new class. It doesn't raise NameError.

The example is NOT equivalent with

autoload(:C, "load_c")
C # =3D> NameError

If you try it you get value 1, not a NameError because lookup algorithm for=
 constant read is different from the one used in class definition:

C:\Temp>type load_c.rb
module M
  C =3D 1
end

class F
  include M
end

C:\Temp>irb
irb(main):001:0> class F
irb(main):002:1>   autoload(:C, "load_c")
irb(main):003:1>   C
irb(main):004:1> end
=3D> 1
irb(main):005:0> exit

C:\Temp>irb
irb(main):001:0> class F
irb(main):002:1>   autoload(:C, "load_c")
irb(main):003:1>   class C
irb(main):004:2>   end
irb(main):005:1> end
NameError: uninitialized constant F::C
        from (irb):3
irb(main):006:0>

Tomas

-----Original Message-----
From: Nobuyoshi Nakada [mailto:nobu / ruby-lang.org]
Sent: Sunday, October 05, 2008 8:28 AM
To: ruby-core / ruby-lang.org
Subject: [ruby-core:19129] Re: Autoload and class definition

> Now, let's do the same using constant auto-loading instead:
>
> class F
>   autoload(:C, "load_c");
>
>   class C  # =3D> uninitialized constant F::C (NameError)
>   end
> end
>
> It seems that autoload repeats the same constant resolution
> semantics that triggered it (otherwise C would be found in M
> and TypeError would be raised), yet it raises error if the
> constant is not found. This seems to be a bug. I would expect
> no exception.

That autoload declares C must be defined in load_c, but it
isn't.  And class definition checks if the class is already
defined, to reopen it.

Your example is same as:

  autoload(:C, "load_c")
  C # =3D> NameError

--
Nobu Nakada
c