From: Luke Kanies Date: 2006-08-02T08:03:22+09:00 Subject: Re: Strategies for autoloading ruby files ara.t.howard@noaa.gov wrote: > > this may be helpful (or not! ;-)) > > http://codeforpeople.com/lib/ruby/dynaload/ Clearly, I should always check your codebase before asking any questions. :) > i use it for plugins in my satellite processing system like so > > > class PlugIn > def self.inherited other > Dynaload::export self, 'plugin' => true > end > end > > > then plugins are written something like > > > require 'plugin' > > class MyPlugIn < Plugin > # > # stuff > # > end > > > then, assume that is saved in myplugin.rb, then i can do > > > require 'dynaload' > > loaded = Dynaload::dynaload 'myplugin.rb' > > plugins = loaded.select{|klass, attributes| attributes['plugin'] == true} > > > make sense? Yeah. Half of my question, though, was figuring out which files to load, although that's easy enough to resolve, I guess. I'm assuming it does get moderately expensive as I do more of these, but I normally load by name (e.g., look for 'puppet/type/package/dpkg.rb' anywhere in the search path), and only sometimes load all files in all search directories. I started out using the self.inherited method for all of this kind of work, but it just got too cumbersome eventually, at least partially because the object passed to 'inherited' has not yet been initialized. That is, with the following code: class MyClass < ParentClass @name = :myclass end When 'inherited' is called, '@name' is not set, so you have to stick it in an array and then do shenanigans later to make it act like a hash. This is clearly a trivial example, because 'name' can (usually) be autodetermined from the class name, but you also can't test whether methods are defined, for instance. With the 'new' methods, I can do any initialization on the class that I want (e.g., setting @name using accessor methods), class_eval the passed block, and then do any analysis I need. See [1]. When I switched from using inherited to creating 'new' methods, it unleashed a flood of productivity. I was astounded -- I barely slept for two weeks because so much was suddenly so easy when it had been so difficult for so long. 1 - http://reductivelabs.com/downloads/puppet/apidocs/classes/Puppet/Type.html#M000597 Notice all of the other 'new*' methods around this one. I use it everywhere and I love it. -- Neonle will continue to be rude, and will nretend that you had a small stroke which makes you unable to say or see the letter "n". Stunid nractical joke, if you ask me. Bunch of noon-heads, huh? -- Fred Barling, Humorscope --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com