require_all
A wonderfully simple way to load your code.
Tired of futzing around with require statements everywhere, littering your code
with require File.dirname(__FILE__)
crap? What if you could just
point something at a big directory full of code and have everything just
automagically load regardless of the dependency structure?
Wouldn’t that be nice? Well, now you can!
require 'require_all'
You can use require_all in a multitude of different ways.
The easiest way to use require_all is to just point it at a directory containing a bunch of .rb files:
require_all 'lib'
This will find all the .rb files under the lib directory (including all subdirectories as well) and load them.
The proper order to in which to load them is determined automatically. If the dependencies between the matched files are unresolvable, it will throw the first unresolvable NameError.
You can also give it a glob, which will enumerate all the matching files:
require_all 'lib/**/*.rb'
It will also accept an array of files:
require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file? f }
Or if you want, just list the files directly as arguments:
require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
Still have the require File.dirname(__FILE__)
blues? The require_all gem also
provides a require_rel statement which requires files to relative to the
current file. So you can replace statements like:
require File.dirname(__FILE__) + '/foobar'
with just a simple require_rel:
require_rel 'foobar'
Even better, require_rel still has the full power of require_all, so you can use require_rel to load entire directories of code too. If “foobar” is a directory this will load all the .rb files found under that directory with automagic dependency handling.
The difference between require_all
and require_rel
is that the former loads from the
working directory and latter from the directory relative to the __FILE__
.
So, if your working directory is let’s say /home, and there is /lib/a/b.rb and /lib/c.rb, then
require_all "lib/"
loads every ruby file from the lib directory in the working directory (pwd)
and in /lib/c.rb require_rel "a/"
loads every ruby file from the a/ directory not paying any attention
to the working directory itself.
It’s recommended to use require_rel since it is not affected by the working directory.
Also load_all and load_rel methods exist to use Kernel#load instead of Kernel#require!
It’s just that easy! Code loading shouldn’t be hard.
autoload_all
There’s also a methods for performing autoloading – what a bargain! Similar syntax is used as for require and load methods although some things have to be kept in mind:
- Directory and file names have to reflect namespaces and/or constant names – e.g.
a file called my_file.rb in directories dir1/dir2 has to be defined like this:
module Dir1 module Dir2 class MyFile end end end
autoload_all File.dirname(__FILE__) + "/dir1"
- A :base_dir option has to be specified if loading directories or files from some other location than top-level directory.
autoload_all File.dirname(<i><span class="caps">FILE</span></i>) + “/dir2/my_file.rb”,
:base_dir => File.dirname(<i><span class="caps">FILE</span></i>) + “/../dir1” # top-level namespace starts from dir1
- All namespaces will be created dynamically by autoload_all – this means that defined?(Dir1) will return “constant” even if my_file.rb is not loaded!
Of course there’s also an autoload_rel method:
autoload_rel "dir2/my_file.rb", :base_dir => File.dirname(__FILE__) + "/../dir1"
If having some problems with autoload_all or autoload_rel then set $DEBUG to true to see how files are mapped to their respective modules and classes.
Methodology (except for autoload_all|rel)
I didn’t invent the approach this gem uses. It was shamelessly stolen from Merb (which apparently stole it from elsewhere). Here’s how it works:
- Enumerate the files to be loaded
- Try to load all of the files. If we encounter a NameError loading a particular file, store that file in a “try to load it later” list.
- If all the files loaded, great, we’re done! If not, go through the “try to load it later” list again rescuing NameErrors the same way.
- If we walk the whole “try to load it later” list and it doesn’t shrink at all, we’ve encountered an unresolvable dependency. In this case, require_all will rethrow the first NameError it encountered.
Questions? Comments? Concerns?
You can reach the author on github or freenode: “jarm0”
Or by email: [email protected]
Got issues with require_all to report? Post ’em here:
License
require_all was done originally by Tony Arcieri who asked me to maintain the gem.
MIT (see the LICENSE file for details)