From: Pit Capitain Date: 2002-02-22T21:13:54+09:00 Subject: Re: recursive require On 22 Feb 2002, at 19:59, Yohanes Santoso wrote: > > 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 If you carefully look at the output, you'll notice that "Xyz" and "Abc" are printed with uppercase letters. So it's not the method "name" declared in abc.rb and xyz.rb that is called, but the method Module#name. All you get in abc.rb is the empty class Xyz and vice versa. Regards, Pit