From: "Michael T. Richter" Date: 2008-07-23T22:03:59+09:00 Subject: Re: circular 'require' --=-9L+RueHhSMgoBQIE9+CP Content-Type: text/plain Content-Transfer-Encoding: 7bit On Wed, 2008-07-23 at 20:39 +0900, Calamitas wrote: > test1.rb is executed twice, but what probably caused the error in the > OP's original program (the OP pared it down to a point where it > actually did "work") is the fact that "the rest of test1.rb" is > executed before "the rest of test2.rb". It can probably be solved by > doing "ruby -r test1.rb" instead, but that's icky. Generally, if I > *were* to use circular requires, I'd never put "executable code" in > either file, just "declarations". I don't use them though. Not anymore > after I've been bitten a few times. Here's what the OP was talking about, I suspect, taken down to its briefest possible example: $ cat test1.rb require 'test2' class NeededInTest2 @@funny_stuff = NeededInTest1.new end michael@isolde:~/junk$ cat test2.rb require 'test1' class NeededInTest1 @@funny_stuff = NeededInTest2.new end michael@isolde:~/junk$ ruby test1.rb ./test1.rb:3: uninitialized constant NeededInTest2::NeededInTest1 (NameError) from /home/michael/software/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require' from /home/michael/software/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require' from ./test2.rb:1 from /home/michael/software/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require' from /home/michael/software/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require' from test1.rb:1 This is the problem boiled down to its essence. There is no easy way out of this either that doesn't either a) mangle the architecture or b) fail. You can't use something like C/++'s "include guards" to get around this, nor can you use any kind of super-class elevation trick or the like. What would be needed to solve this issue would be for ruby to have a proper sense of modules and do linkage across those instead of treating the entire tree of requires as, in effect, one ginormous text file. -- Michael T. Richter (GoogleTalk: ttmrichter@gmail.com) Theory is knowledge that doesn't work. Practice is when everything works and you don't know why. (Hermann Hesse) --=-9L+RueHhSMgoBQIE9+CP--