From: Brian Palmer Date: 2006-08-02T07:04:19+09:00 Subject: Re: Strategies for autoloading ruby files You can use `Module.const_missing` to autoload a file the first time the class in that file is mentioned in the code. So you could write an `Object.const_missing(name)` method that translates the constant into a file name of an expected format, then load that file on-the-fly. Amusingly, this is exactly the code example that's given in the Pickaxe version 2 under Module.const_missing, along with a note that the code is in very poor style and is a "perverse kind of autoload functionality". :) -- Brian On Aug 1, 2006, at 3:53 PM, Luke Kanies wrote: > Hi all, > > Following up on my post about complicated problems, here's a much > simpler problem that I need to tackle. > > There are many parts of my framework that are extensible, and I > want people to be able to create extensions without modifying the > core framework, akin to how you don't have to specifically load > each model and controller in Rails. > > Is there a relatively standard way of doing that with Ruby? This > is what I'm doing in one of my projects, Facter[1]: > > # Now see if we can find any other facts > $:.each do |dir| > fdir = File.join(dir, "facter") > if FileTest.exists?(fdir) and FileTest.directory?(fdir) > Dir.glob("#{fdir}/*.rb").each do |file| > # Load here, rather than require, because > otherwise > # the facts won't get reloaded if someone calls > # "loadfacts". Really only important in testing, > # but, well, it's important in testing. > begin > load file > rescue => detail > warn "Could not load %s: %s" % > [file, detail] > end > end > end > end > > I've tried further copying Rails by checking that loaded files > create the objects it seems they should create (e.g., here they > should create a fact named after the file), but users constantly > complained about those warnings. > > Is this the best way? Any other recommended mechanisms? > > I'd actually like the autoloading to work either on-demand (e.g., > look for 'facter/myfact' if I ask for the 'myfact' fact), or load > all of them right now. > > Hmm. In fact, it'd be ideal if you could write a module that > allows you to register autoload paths (e.g., I'd use 'facter', > 'puppet/type', 'puppet/type/package', 'puppet/type/service', and > lots more), and then just have hooks into that module so that you > could autoload all instances, or just load a specific name. > > 1 - http://reductivelabs.com/projects/facter > > -- > The Number 1 Sign You Have Nothing to Do at Work... > The 4th Division of Paperclips has overrun the Pushpin Infantry > and General White-Out has called for a new skirmish. > --------------------------------------------------------------------- > Luke Kanies | http://reductivelabs.com | http://madstop.com > >