Re: Unnecessary exception raised in AS::Dependencies.load_missing_constant

Andrew White
2010-02-15T12:26:25Z

On 15 Feb 2010, at 01:32, Yehuda Katz wrote:

> What I''d like to understand is what the various cases are that
could trigger this branch. In particular, I''m interested in
understanding the cases without reference to ActiveRecord (which it looks like
you and others have started to do).

Actually looking at it again this morning it''s class_eval and the
difference between class Foo::Bar; end and class Foo; class Bar; end; end. that
seems to be the trigger, e.g:

>> module Foo; class Baz; end; end
=> nil
>> module Foo; class Bar; def self.baz; Baz; end; end; end
=> nil
>> Foo::Bar.baz
=> Foo::Baz
>> Foo::Bar.class_eval { Baz }
NameError: uninitialized constant Foo::Bar::Baz
	from (irb):6:in `block in irb_binding''
	from (irb):6:in `class_eval''
	from (irb):6
	from /Users/andyw/.rvm/rubies/ruby-1.9.1-p378/bin/irb:17:in
` ''
>> module Foo; Bar.class_eval { Baz }; end
=> Foo::Baz

This is why it''s mainly seen when dealing with ActiveRecord - the
compute_type method in AR::Base seems to be the only place where this pattern is
used. Basically, there''s a mismatch between load_missing_constant and
Ruby''s constant resolution. Personally I''d consider this a bug
in Ruby - I don''t see any valid reason for the two different scopes.


Andrew White