From: Shadowfirebird Date: 2008-07-23T18:48:13+09:00 Subject: circular 'require' Hi, I'm new to ruby and I'm really enjoying it. However, there is one thing... I gather from this list and my poor efforts that ruby does not like files that 'require' each other circularly. I can find nothing in the documentation about this, though; in fact, the description of require seems to actually rule it out. Can anyone explain what is going on here? (This happens in 1.8 and 1.9 and JRuby 1.0. So it's definitely me, not Ruby, that has the problem.) To flesh things out a little: 'require' is supposed to load files, unless they have been already loaded. It stores an array of loaded files in $" for this purpose. Okay, suppose we have two programs in two different files: # test1.rb require "test2.rb" class One def self.testone(); Two.whoistwo; end def self.whoisone(); puts "class one"; end end # test2.rb require "test1.rb" class Two def self.testtwo(); One.whoisone; end def self.whoistwo(); puts "class two"; end end >ruby -w test1.rb ./test1.rb:4: unititialized constant Two (NameError) Why does this happen? I would have thought that test1 would load test2; then test2 would go to load test1 but find it already loaded. Apparently what happens is that test2 doesn't get loaded at all. OTOH if I replace the 'require' with a homebrew, like this: # test1.rb $" << "test1.rb" load("test2.rb") if !$".include?("test2.rb") ... ...then everything works fine, which seems to imply that 'replace' isn't behaving as described in the slightest. Anyone that can provide a description of what replace actually does while staying high-level enough for my head not to spin, will earn my undying gratitude. Ta, Shadowfirebird. -- 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