From: Shadowfirebird Date: 2008-07-23T20:47:58+09:00 Subject: Re: circular 'require' *Is* there any executable code in my two example programs, other than the "require"s? On Wed, Jul 23, 2008 at 12:39 PM, Calamitas wrote: > On Wed, Jul 23, 2008 at 12:18 PM, Stefano Crocco > wrote: >> On Wednesday 23 July 2008, Shadowfirebird wrote: >>> >ruby -w test1.rb >>> >>> ./test1.rb:4: unititialized constant Two (NameError) >> >> require 'test1.rb' >> >> is executed: >> >> * the file test1.rb is read and parsed >> * starts the execution of test1.rb >> * the line require 'test2.rb' is executed. This means: >> * the file test2.rb is read and parsed >> * starts the execution of test2.rb >> * the line require 'test1.rb' is executed. This means: >> * the file test1.rb is read and parsed >> * starts the execution of test1.rb >> * the line require 'test2.rb' is executed. This means: >> ... > > Actually, I'm neither seeing the error the OP got, neither the endless > loop you seem to get. Looks like a file that is required gets added to > $" before its contents is executed, and that a file passed on the > command line -- like the OP did, so not using the -r switch -- is not. > So basically the execution is as follows: > > * the file test1.rb is read and parsed > * starts the execution of test1.rb > * the line require 'test2.rb' is executed. This means: > * the file test2.rb is read and parsed > * adds "test2.rb" to $" > * the line require 'test1.rb' is executed. This means: > * the file test1.rb is read and parsed > * adds "test1.rb" to $" > * the line require 'test1.rb' is executed. Does nothing as > "test1.rb" is in $" > * The rest of test1.rb is executed > * The rest of test2.rb is executed > * The rest of test2.rb is executed again > > 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. > > Peter > > -- Me, I imagine places that I have never seen / The colored lights in fountains, blue and green / And I imagine places that I will never go / Behind these clouds that hang here dark and low But it's there when I'm holding you / There when I'm sleeping too / There when there's nothing left of me / Hanging out behind the burned-out factories / Out of reach but leading me / Into the beautiful sea