From: Travis Whitton Date: 2003-09-16T04:08:25+09:00 Subject: Re: Problem Dynamically Loading a Module Guy, Thanks very much! That solved my problem perfectly. I love the Ruby community. Travis In article <200309151635.h8FGZgT05555@moulon.inra.fr>, ts wrote: >>>>>> "T" == Travis Whitton writes: > > > Try this > >T> class DynamicInclude >T> def initialize(dynmod) >T> DynamicInclude.load_module(dynmod) > > extend(Object.const_get(dynmod.to_s)) > >T> end > >T> def DynamicInclude.load_module(modname) >T> load "#{modname}.rb" >T> include Object.const_get(modname.to_s) > > you don't need this include > >T> end >T> end > > Now if you are sure that these previous module don't exist you can write > > svg% cat b.rb > #!/usr/bin/ruby > > class DynamicInclude > def initialize(dynmod) > DynamicInclude.load_module(dynmod) > extend(Object.instance_eval { remove_const(dynmod.to_s) }) > end > > def DynamicInclude.load_module(modname) > load "#{modname}.rb" > end > end > > d = DynamicInclude.new("Hello") > d.greeting() # prints Hello > e = DynamicInclude.new("Goodbye") > e.greeting() # prints Goodbye > d.greeting() # prints Goodbye > svg% > > svg% b.rb > Hello > Goodbye > Hello > svg% > > The load is made in Object, then the constant is removed to don't pollute > Object > > > Guy Decoux >