From: ES Date: 2005-02-20T10:12:47+09:00 Subject: Re: Dynamically loading and executing within a new module? On 2/19/2005, "Lloyd Zusman" wrote: >Lloyd Zusman writes: > >> ES writes: >> >>> >>> [ ... ] >>> >>> If you don't just want to write an actual module and then call its #init, >>> you could do something like this. Somewhat pseudo: >>> >>> Thread.new do >>> if fname >>> mod = eval fname.capitalize >>> data = File.new(fname, 'r') do |f| >>> f.read >>> end >>> mod.module_eval data >>> mod.send :init, args >>> # ... >>> end >>> end >>> >>> Or sumptin :) >> >> Thanks. This makes sense. >> >> However, the "eval fname.capitalize" part doesn't create a new module, >> and that's part of what is confusing me about all this. Given a string >> "Foo", what do I do to it in order to create a module object with that >> name? > >Actually, I now realize that I don't need a bona fide named module, but >rather, just a way to associate a name with a module. Therefore, I >think that I can do it more or less like this: > > modhash = Hash.new > if fname then > if modhash.has_key?(fname) then > mod = modhash[fname] > else > mod = Module.new > modhash[fname] = mod > end > data = File.new(fname, 'r') do |f| > f.read > end > mod.module_eval data > mod.send :init, args > end > >Does this make sense? Yes, but I'm not sure if I understand why you're doing this in the first place. Why don't you just create a named module, store it in a file and load it when requested instead of just storing the code that'll go in the module? > Lloyd Zusman E