From: Ron Jeffries Date: 2002-02-22T19:39:45+09:00 Subject: Re: recursive require On Fri, 22 Feb 2002 01:16:02 GMT, Dave Thomas wrote: >Ron Jeffries writes: > >> Note that the code classes are defined twice. If I do it the other way >> around, running the test file, then it happens the other way around: the >> tests are loaded twice and the test files defined twice. >> >> Either way it's weird. Worst of all, the program runs differently >> depending which file I load first. > >Ron: > >Ruby won't require or load the same file twice. However, executing the >file as a main program doesn't count as loading or requiring it, so: > > code.rb > require "test" > > puts "code" > > > test.rb > require "code" > > puts "test" > > >When you say 'ruby code.rb', it does > > require "test" - not yet loaded > in test.rb > require "code" - not yet loaded (it thinks) > in code.rb (again) > require "test" - already loaded > puts "code" > back out to test.rb > puts "test" > back out to code.rb > puts "code" That's what it does, all right. IMO it's a bug or at least very odd. For example, it will init class variables twice. It also apparently compiles the upper file twice, which seems, um, sufficient ... Anyway, what's the /right/ way to have code and tests in the same file without setting up each project separately, in a simple dumb way suitable to the likes of me? Thanks, Ronald E Jeffries http://www.XProgramming.com http://www.objectmentor.com I'm giving the best advice I have. You get to decide whether it's true for you.