Nobu's point is that your inner "class C" accesses C under the 
assumption that "load_c" will define it. However, this assumption is not 
true as "load_c" will not define C within the scope of F. In my opinion, 
the question is which semantics are more important. Is it more important 
to signal the user that he actually made a mistake by declaring a file 
to load a constant which it in fact did not or is it more important to 
keep the semantics of the class definition to check whether the class is 
already defined and to create a new one if it does not? I personally 
consider the latter more important, i.e. a class definition should never 
raise a NameError.

My two cents,
Tilman

Tomas Matousek wrote:
> "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 creates a new class. It doesn't raise NameError.
>
> The example is NOT equivalent with
>
> autoload(:C, "load_c")
> C # => 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 = 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
> => 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  # => 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 # => NameError
>
> --
> Nobu Nakada
> c
>
>
>
>