From: Trans Date: 2006-08-22T14:05:06+09:00 Subject: Re: using a module at the toplevel doesn't work Yukihiro Matsumoto wrote: > Hi, > > In message "Re: using a module at the toplevel doesn't work" > on Tue, 22 Aug 2006 12:40:11 +0900, "Trans" writes: > > |Well, is there anyway to turn toplevel effects on Object off? I have a > |real scenario where it causes issues. > | > | module X > | def self.method_missing( name, *args ) > | if name ... > | if require 'foo' > | send( name, *args ) > | ... > | > |Now if some method is defined at toplevel it can screw up my module X > |lazy loader. > > Can you show me working (well, I mean non-working) example. I am not > sure how above example relates to toplevel effects. Sure. I have a bunch of build scripts that can be resused in other scripts. Typcially people will reuse then threw one-off scripts that simple define methods at the top level. E.g. something like def test Script.test end Now the Script module uses lazy loading in order to keep it fast --no sense in loading module's you don't need: module Script extend self # Some tasks belong to variant scripts. TASK2SCRIPT = { :version => :revision, :changelog => :revision } # When a built-in task is called it's script is dynamically loaded. def method_missing( meth, *args, &blk ) file = TASK2SCRIPT[meth] || meth begin require "sake/#{file}" if respond_to?(meth) send( meth, *args, &blk ) else super end rescue LoadError super end end end As you can see my particlar 'test' example will cause an infinite loop because the toplevel definition is being added to all objects. :( Thanks for looking at this, T.