George Ogata
2/4/2006 9:34:00 AM
Michael Judge <mjudge@surveycomplete.com> writes:
> File a.rb
> require "b"
>
> class A < Array
> # Do nothing
> end
>
> File b.rb
> require "a"
>
> class B < A
> # Do nothing
> end
>
> Where's the brutally obvious mistake I made? Why would A be an
> uninitialized constant?
Because it hasn't been defined when "class B < A" is evaluated.
Here's what happens, step by step:
- irb evaluates: require "a"
+ ruby parses the 'a.rb' file into an internal representation, doing
syntax checking along the way
+ ruby evaluates the first expression: require "b"
* ruby parses the 'b.rb' file into an internal representation,
doing syntax checking along the way
* ruby evaluates the first expression: require "a". As 'a.rb' has
already been required, this does effectively nothing (see `ri
require').
* ruby evaluates the next expression: class B < A ... end. As A
is not defined, ruby raises the exception you see.
Why does 'a.rb' require 'b.rb' ? Circular requires are a bad thing.
You'll need to break it up.
One option is to just have the toplevel file just have a whole
sequence of requires, and have each source file define what it needs
to without messing around with any requires whatsoever.
Hope this helps.