From: Yohanes Santoso Date: 2002-02-22T19:59:48+09:00 Subject: Re: recursive require Ron Jeffries writes: > On 21 Feb 2002 16:02:35 -0800, Yohanes Santoso > wrote: > > >2. Use forward declaration > >test.rb: > >class Code end > >Code.something() > > > >Personally, I like solution #2 because it's easy and fast. I put all > >the forward declarations in a separate file which is 'require'd by > >both code.rb and test.rb. > > Thanks for answering, but I'm araid I don't understand this idea. If > test.rb doesn't require code.rb, how does this work? If the two files > don't require each other, how can I run either one and get the tests to > run (which is what I'm trying to accomplish)? > Here are three files, abc.rb, xyz.rb, and global.rb. abc contains class Abc, and xyz contains class Xyz. Abc and Xyz are inter-dependant. Putting "require 'xyz'" in abc.rb and "require 'abc'" in xyz.rb will not work as you pointed out. So, pre-declare class Abc in xyz.rb and pre-declare class Xyz in abc.rb. However, rather than putting the declaration everywhere, collect them into a file: global.rb. Then, from abc.rb and xyz.rb require global.rb global.rb: class Abc end class Xyz end abc.rb: require 'global' class Abc def Abc.name "abc" end def whoami puts "abc knows xyz:" puts Xyz.name puts "abc tells the truth" end end a = Abc.new a.whoami xyz.rb: require 'global' class Xyz def Xyz.name "xyz" end def whoami puts "xyz knows abc:" puts Abc.name puts "xyz tells the truth" end end x = Xyz.new x.whoami jenny:~/tmp> ruby abc.rb abc knows xyz: Xyz abc tells the truth jenny:~/tmp> ruby xyz.rb xyz knows abc: Abc xyz tells the truth