From: Logan Capaldo Date: 2006-08-18T10:24:19+09:00 Subject: Re: using a module at the toplevel doesn't work On Aug 17, 2006, at 3:24 PM, Pit Capitain wrote: > Trans schrieb: >> Unfortuately when I use this to make a Rake command-line emulator I >> want to use the module at the toplevel and can't. > > Tom, I think I still don't get what your problem is. If you want to > use a module at the toplevel just include it in Object. > > The module: > > module M > def val *args > if args.empty? > @val > else > @val = args[ 0 ] > end > end > end > > I used some instance variables here, because you wrote about them > in the answer to Matz. > > Make it available at the toplevel (and everywhere else): > > class Object > include M > end > > p val # => nil > val "main" > p val # => "main" > > It's also available at the class level: > > class Q > p val # => nil > val "Q" > p val # => "Q" > end > > HTH > > Regards, > Pit > You are over simplifying Trans.'s problem. He wants dynamic generated methods scoped to a module, or optionally scoped to the top level. You can't use #define_method at the top level because instances don't respond to it. You can't use the toplevel singleton class as I suggested because a) it's a pain in the neck and b) it doesn't inherit the right way. Trans, what about: # Warning evilness ensuing: def main.define_method(*args, &block) Kernel.module_eval{ define_method(*args, &block) } end