From: ara.t.howard@... Date: 2006-08-02T08:15:07+09:00 Subject: Re: Strategies for autoloading ruby files On Wed, 2 Aug 2006, Luke Kanies wrote: > 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. indeed. that one's pretty project specific. in my case i have a well known location(s) for plugins... > 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]. hmmm. that's interesting. another approach would be module Plugable def self.included other ... end end class MyPlugin include Plugable end i've not run into that issue though. > 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. yes - i've got a fair number of class factories lying around myself. Class.new is powerfull since you can easily parameterize your classes. on a related note i used 'traits' alot with this sort of thing since it avoids needing to initialize all those class vars directly down the inheritence chain. i'll read over some over of you code but gotta run to get the kid... ciao. -a -- we can never obtain peace in the outer world until we make peace with ourselves. - h.h. the 14th dali lama