[ANN] require_all 1.1.0: the final nail on the coffin of requireFile.dirname(__FILE__)

T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

require File.dirname(__FILE__) seems like an incredibly common idiom. But
boy is it ugly! Ruby is a beautiful language so why should we litter it
with ugly statements like that?

Now you don't have to! At the suggestion of Roger Pack I've added a new
feature to the require_all gem. Introducing require_rel. Here's how it
works... let's start with an ugly require statement:

require File.dirname(__FILE__) + 'foobar'

Yeech. Can we do better? Yes we can:

require_rel 'foobar'

Done! The require_rel statement works relative to the directory the caller
is located in. Bye bye File.dirname(__FILE__)!

Even better, require_rel has the same code loading powers as require_all.
If 'foobar' is a directory, it will look for all the .rb files under the
foobar directory and use require_all's automagic dependency resolution to
load them in the proper order. (In case anyone is wondering, if you have a
'foobar' directory and a 'foobar.rb' require_all will prefer the directory
over the .rb file). Or you can give require_rel a custom glob which works
relative to the current file.

If you'd like to learn more, check it out on github:

http://github.com/tarcieri/require_all/tree/master
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top