# Strategies for autoloading ruby files

**URL:** <https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521>\
**Category:** ruby-talk\
**Created:** [1 August 2006 21:53 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521 "2006-08-01T21:53:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_A\_Kanies](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@Luke\_A\_Kanies](https://rubytalk.org/u/Luke_A_Kanies)\
**Post date:** [1 August 2006 21:53 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/1 "2006-08-01T21:53:36Z")

</div>

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]:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Now see if we can find any other facts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$:.each do |dir|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fdir = File.join(dir, "facter")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if FileTest.exists?(fdir) and FileTest.directory?(fdir)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.glob("#{fdir}/\*.rb").each do |file|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Load here, rather than require, because otherwise  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the facts won't get reloaded if someone calls  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# "loadfacts". Really only important in testing,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# but, well, it's important in testing.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;load file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> detail  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warn "Could not load %s: %s" %  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[file, detail]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](http://reductivelabs.com/projects/facter)

> **···**
>
> --  
> The Number 1 Sign You Have Nothing to Do at Work...  
> &nbsp;&nbsp;&nbsp;&nbsp;The 4th Division of Paperclips has overrun the Pushpin Infantry  
> &nbsp;&nbsp;&nbsp;&nbsp;and General White-Out has called for a new skirmish.  
> ---------------------------------------------------------------------  
> Luke Kanies | [http://reductivelabs.com](http://reductivelabs.com) | [http://madstop.com](http://madstop.com)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [1 August 2006 22:02 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/2 "2006-08-01T22:02:23Z")

</div>

this may be helpful (or not! ;-))

&nbsp;&nbsp;&nbsp;[http://codeforpeople.com/lib/ruby/dynaload/](http://codeforpeople.com/lib/ruby/dynaload/)

i use it for plugins in my satellite processing system like so

&nbsp;&nbsp;&nbsp;class PlugIn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.inherited other  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dynaload::export self, 'plugin' =\> true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

then plugins are written something like

&nbsp;&nbsp;&nbsp;require 'plugin'

&nbsp;&nbsp;&nbsp;class MyPlugIn \< Plugin

> **···**
>
> On Wed, 2 Aug 2006, 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]:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Now see if we can find any other facts  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$:.each do |dir|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fdir = File.join(dir, "facter")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if FileTest.exists?(fdir) and FileTest.directory?(fdir)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.glob("#{fdir}/\*.rb").each do |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Load here, rather than require, because otherwise  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the facts won't get reloaded if someone calls  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# "loadfacts". Really only important in testing,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# but, well, it's important in testing.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;load file  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> detail  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warn "Could not load %s: %s" %  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[file, detail]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](http://reductivelabs.com/projects/facter)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# stuff  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;end
> 
> then, assume that is saved in myplugin.rb, then i can do
> 
> &nbsp;&nbsp;&nbsp;require 'dynaload'
> 
> &nbsp;&nbsp;&nbsp;loaded = Dynaload::dynaload 'myplugin.rb'
> 
> &nbsp;&nbsp;&nbsp;plugins = loaded.select{|klass, attributes| attributes['plugin'] == true}
> 
> make sense?
> 
> the reason it's at 0.0.0 is that it just worked for me - been using in  
> production for a long time.
> 
> cheers.
> 
> -a  
> --  
> we can never obtain peace in the outer world until we make peace with  
> ourselves.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Brian\_Palmer2](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@Brian\_Palmer2](https://rubytalk.org/u/Brian_Palmer2)\
**Post date:** [1 August 2006 22:04 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/3 "2006-08-01T22:04:19Z")

</div>

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]:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Now see if we can find any other facts  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$:.each do |dir|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fdir = File.join(dir, "facter")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if FileTest.exists?(fdir) and FileTest.directory?(fdir)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.glob("#{fdir}/\*.rb").each do |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Load here, rather than require, because otherwise  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the facts won't get reloaded if someone calls  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# "loadfacts". Really only important in testing,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# but, well, it's important in testing.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;load file  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> detail  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warn "Could not load %s: %s" %  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[file, detail]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](http://reductivelabs.com/projects/facter)
> > 
> > --  
> > The Number 1 Sign You Have Nothing to Do at Work...  
> > &nbsp;&nbsp;&nbsp;The 4th Division of Paperclips has overrun the Pushpin Infantry  
> > &nbsp;&nbsp;&nbsp;and General White-Out has called for a new skirmish.  
> > ---------------------------------------------------------------------  
> > Luke Kanies | [http://reductivelabs.com](http://reductivelabs.com) | [http://madstop.com](http://madstop.com)

---

<div class="post-metadata">

**Author:** ![F\_Senault](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@F\_Senault](https://rubytalk.org/u/F_Senault)\
**Post date:** [2 August 2006 09:50 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/4 "2006-08-02T09:50:05Z")

</div>

I don't know about recommended, but I've made two or three programs  
using plugins, and here's my method, in the main script :

Dir.glob($conf[:path]) do |f|  
&nbsp;&nbsp;$mtime = File.mtime(f)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "#{f}"  
&nbsp;&nbsp;rescue LoadError =\> err  
&nbsp;&nbsp;&nbsp;&nbsp;puts err  
&nbsp;&nbsp;end  
end  
Plugin.plugins.each |p| do  
&nbsp;&nbsp;...  
end

class Plugin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@plugins =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@mtimes = {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def Grapher.inherited(c)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@plugins.push(c)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@mtimes[c.to\_s] = $mtime  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def Plugin.plugins  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@plugins  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def Plugin.mtimes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@mtimes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

And the plugins just have to :

class LittlePlugin \< Plugin  
&nbsp;&nbsp;...  
end

Note : I needed here to know the modification time of the plugin to  
eventually reinitialize some data. I think that using a global variable  
is more than clunky. Any thoughts on a better way ?

Fred

> **···**
>
> Le 01 août à 23:53, Luke Kanies a écrit :
> 
> > Is this the best way? Any other recommended mechanisms?
> 
> --  
> The DBAs will be whining because when you kill -9 Oracle, you get bits  
> and pieces left all over the place, and when re-started Oracle gets all  
> squeamish and frightened about what happened to it's predecessor. (Its  
> very literal pre-decessor...) (Dan Holdsworth in the SDM)

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [1 August 2006 22:11 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/5 "2006-08-01T22:11:45Z")

</div>

Ruby has a function for this though:

$ ri -T Kernel#autoload  
-------------------------------------------------------- Kernel#autoload  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;autoload(module, filename) =\> nil

> **···**
>
> On Aug 1, 2006, at 5:04 PM, Brian Palmer wrote:
> 
> > 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.
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Registers filename to be loaded (using Kernel::require) the first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time that module (which may be a String or a symbol) is accessed.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;autoload(:MyModule, "/usr/local/lib/modules/my\_module.rb")
> 
> James Edward Gray II

---

<div class="post-metadata">

**Author:** ![Luke\_A\_Kanies](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@Luke\_A\_Kanies](https://rubytalk.org/u/Luke_A_Kanies)\
**Post date:** [1 August 2006 22:22 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/6 "2006-08-01T22:22:33Z")

</div>

Brian Palmer wrote:

> 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.

Ironically, I use almost no constants in my code. Parent classes get constants, but subclasses are almost always created dynamically[1], like so:

&nbsp;&nbsp;&nbsp;Puppet::Type.newtype(:user) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;end

Then I always refer to that class by the name 'user' and never by the constant. This is partially because it's easier for me, but also because I've written a custom language (no, not a DSL[2]) for Puppet, and most of those names are exposed in that language. E.g., you create a 'user' type like above, and users can create user elements in Puppet:

&nbsp;&nbsp;&nbsp;user { luke:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment =\> "Luke Kanies",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uid =\> 100,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell =\> bash,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure =\> exists  
&nbsp;&nbsp;&nbsp;}

Puppet supports lots of package types (e.g., dpkg, apt, rpm, darwinports, gems) and service types (init, smf, etc.); I specify the name when I create the class, and users can select those types by name, either in the language or directly in Ruby if they're directly accessing the library.

In other words, my problem is not how to hook into the autoload, it's the guts behind it.

> 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".

1 - I do assign constants to them, but only because class inspection during error printing did not work consistently unless there's a constant assigned to the class. I just tried reproducing this but could not; I don't remember the details well enough, I guess.

2 - See the FAQ, [http://reductivelabs.com/projects/puppet/faq.html](http://reductivelabs.com/projects/puppet/faq.html), the fourth question.

> **···**
>
> --  
> An expert is a person who has made all the mistakes that can be made  
> in a very narrow field. - Niels Bohr  
> ---------------------------------------------------------------------  
> Luke Kanies | [http://reductivelabs.com](http://reductivelabs.com) | [http://madstop.com](http://madstop.com)

---

<div class="post-metadata">

**Author:** ![Luke\_A\_Kanies](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@Luke\_A\_Kanies](https://rubytalk.org/u/Luke_A_Kanies)\
**Post date:** [1 August 2006 23:03 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/7 "2006-08-01T23:03:22Z")

</div>

> this may be helpful (or not! ;-))
> 
> &nbsp;&nbsp;[http://codeforpeople.com/lib/ruby/dynaload/](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
> 
> &nbsp;&nbsp;class PlugIn  
> &nbsp;&nbsp;&nbsp;&nbsp;def self.inherited other  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dynaload::export self, 'plugin' =\> true  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> then plugins are written something like
> 
> &nbsp;&nbsp;require 'plugin'
> 
> &nbsp;&nbsp;class MyPlugIn \< Plugin  
> &nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;# stuff  
> &nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;end
> 
> then, assume that is saved in myplugin.rb, then i can do
> 
> &nbsp;&nbsp;require 'dynaload'
> 
> &nbsp;&nbsp;loaded = Dynaload::dynaload 'myplugin.rb'
> 
> &nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;class MyClass \< ParentClass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@name = :myclass  
&nbsp;&nbsp;&nbsp;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\<thing\>' 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\<thing\>' 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](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.

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> --  
> 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?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- Fred Barling, Humorscope  
> ---------------------------------------------------------------------  
> Luke Kanies | [http://reductivelabs.com](http://reductivelabs.com) | [http://madstop.com](http://madstop.com)

---

<div class="post-metadata">

**Author:** ![Brian\_Palmer2](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@Brian\_Palmer2](https://rubytalk.org/u/Brian_Palmer2)\
**Post date:** [1 August 2006 22:52 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/8 "2006-08-01T22:52:59Z")

</div>

> Ruby has a function for this though:
> 
> $ ri -T Kernel#autoload  
> -------------------------------------------------------- Kernel#autoload  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;autoload(module, filename) =\> nil  
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Registers filename to be loaded (using Kernel::require) the first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time that module (which may be a String or a symbol) is accessed.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;autoload(:MyModule, "/usr/local/lib/modules/my\_module.rb")
> 
> James Edward Gray II

Cool, I didn't realize that was already baked in to Kernel.

> **···**
>
> On Aug 1, 2006, at 4:11 PM, James Edward Gray II wrote:
> 
> On Aug 1, 2006, at 4:22 PM, Luke Kanies wrote:
> 
> > Puppet supports lots of package types (e.g., dpkg, apt, rpm, darwinports, gems) and service types (init, smf, etc.); I specify the name when I create the class, and users can select those types by name, either in the language or directly in Ruby if they're directly accessing the library.
> > 
> > In other words, my problem is not how to hook into the autoload, it's the guts behind it.
> 
> I misunderstood your question, sorry about that.
> 
> -- Brian

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [1 August 2006 23:15 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/9 "2006-08-01T23:15:07Z")

</div>

> 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  
> &nbsp;&nbsp;&nbsp;@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\<thing\>' 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

&nbsp;&nbsp;&nbsp;module Plugable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.included other  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class MyPlugin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Plugable  
&nbsp;&nbsp;&nbsp;end

i've not run into that issue though.

> When I switched from using inherited to creating 'new\<thing\>' 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](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

> **···**
>
> On Wed, 2 Aug 2006, Luke Kanies wrote:  
> --  
> we can never obtain peace in the outer world until we make peace with  
> ourselves.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Luke\_A\_Kanies](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@Luke\_A\_Kanies](https://rubytalk.org/u/Luke_A_Kanies)\
**Post date:** [1 August 2006 23:29 UTC](https://rubytalk.org/t/strategies-for-autoloading-ruby-files/29521/10 "2006-08-01T23:29:59Z")

</div>

> &nbsp;&nbsp;module Plugable  
> &nbsp;&nbsp;&nbsp;&nbsp;def self.included other  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;class MyPlugin  
> &nbsp;&nbsp;&nbsp;&nbsp;include Plugable  
> &nbsp;&nbsp;end

At this point, I'm looking at creating an Autoload class, and classes wanting to autoload will create an instance:

&nbsp;&nbsp;&nbsp;Puppet::Autoload.new(self, "puppet/type")

They'll then be able to store that instance, or retrieve it from the class:

&nbsp;&nbsp;&nbsp;Puppet::Autoload[self].load(:package)

That'll handle the mechanics of the loading. I'll have an additional :loadall method that will search through $:, loading everything that matches.

I want to keep the work around plugin loading -- that is, what to do with the class and how to initialize it -- in the class supporting plugins (i.e., the class with the 'new\<thing\>' method), because most initialization is pretty custom. I suppose I could start with a default method in a module, though; I hadn't thought much about that.

A significant part of this for me is that I have lots of classes that are related to each other -- the Puppet::Type class is related to its subclasses, the Puppet::Type::Package class is related to the Puppet::Type:📦:PkgType classes (ugh, constants are hard), the 'service' class is related to each 'svctype' class, etc. Calling 'new\<thing\>' directly on the related class makes that relationship very explicit and pleasantly clear.

> yes - i've got a fair number of class factories lying around myself.  
> Class.new is powerfull since you can easily parameterize your classes.

I \*love\* Class.new and Module.new. Ruby was great before I discovered them, but with them, I'm definitely hooked. Well, Class.new and class\_eval, anyway.

> 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.

Most of the variables involved are actually class instance variables (which, I understand, others don't use that much, but which I use pervasively and could not live without).

I need to just add traits into my codebase; I think it would simplify a lot of what I'm doing.

/me adds one more item to the todo

> i'll read over some over of you code but gotta run to get the kid...

Comments, positive or negative, are always appreciated.

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> --  
> Today I dialed a wrong number...The other person said, "Hello?" and  
> I said, "Hello, could I speak to Joey?"...  
> They said, "Uh...I don't think so...he's only 2 months old."  
> I said, "I'll wait." -- Steven Wright  
> ---------------------------------------------------------------------  
> Luke Kanies | [http://reductivelabs.com](http://reductivelabs.com) | [http://madstop.com](http://madstop.com)
