From: Damphyr Date: 2003-04-05T23:09:50+09:00 Subject: Re: Plugins (sort off) Brian Candler wrote: > On Sat, Apr 05, 2003 at 08:36:12PM +0900, Damphyr wrote: > >>But then, how do we change the parameter to >>extend to be able to say something like >> >>module = myclass.very_clever_method_takes_string_returns_module(modulename) >>a = Foo.new >>a.extend module > > > That should work just fine - you can pass modules around like any other > object. > > >>In essence what I want is to be able to evaluate a string and find out >>if it matches a symbol (or the texual representation of a symbol to be >>more accurate) in my class space, and then return that symbol or the >>object/class/module for that symbol > > > symbols are global, so "symbol in my class space" doesn't quite make sense. well a symbol I know that belongs (should belong? wants to belong? will belong? :) ) to my classes - I keep mixing words and definitions !very bad!. > Do you mean "does object X have a method of name S"? There are a number of > introspection techniques such as > self.class.instance_methods > self.singleton_methods Actually what I mean is: I have a file MyModule.rb with content: Module MyModule def some_nice_method() end end and another file MyModule2.rb with content Module MyModule2 def some_nice_method() end end and somewhere else I have another script that should (or that I would like it to) be able to take in these files, and any later additions to that pre-specified "plugin repository" and do something like: Class Experiment def doMethod(nameofmethod) self.extend nameofmethod end end the above does not execute (I know, I tried :)) ) and it would be silly if it did (I guess I'm a bit silly for trying :) ). And that is what that very_clever_method_takes_string_returns_module(modulename) should do. It's me as a programmer who knows that MyModule.rb has implicitly the same name as MyModule - now, how can I get the module by the string of it's name? (this sounds nice :) the string of it's name :) ) Probably impossible, (but I haven't looked in DBI or Freebase yet to see how they do their plugins) so I went ahead and did module_eval on raw code. So yes, I'm essentially adding a new method to my class, not a module. What happens when I call module_eval again? From what I gather, the method gets overwritten. OK, I have another description for my question: I know beforehand that there is going to be a module, I have a way to determine the module's name as a string in runtime, I have a way to load that module into memory, but I have no way to call/extend/include the module because I don't know it's name (or I can't create a Module object from it's name). Did I confuse everyone? I'll go away now and look some more... :) V.- -- http://www.freemail.gr - ������ �������� ������������ ������������.