$:is a way-too-Perl-y special variable alias for
$LOAD_PATH. You could change it like this:
The upside: you're now doing something more comprehensible. The downside: that "something" is editing an all-caps global. That doesn't feel wrong to you?
This code pops the local directory name (relative, not absolute) onto the load path. Then you can require filenames as relative paths. This is not such a good idea.
Say you've got a gem which provides an interface to the Twitter API, and this gem uses this convention. This is just an example; I'm not thinking of any real code out there on the githubs. Say you're enabling your web app with some Twitter-related functionality. Maybe you've got files like
app/models/twitter.rb. The problem comes not with this idiom, but from what it makes possible. It enables you to do this:
In this particular example, that shit breaks all over the fucking place.
Yay! I saved myself the effort of typing an absolute path and/or code which would generate an absolute path! And in the process, I guaranteed myself unexpected load bugs. The code attempts to load
app/models/twitterand you get
MethodNotFoundErrors and similar problems. This idiom is only a good idea if your filenames are like the Highlander and there can be only one.
You can make it a little nicer:
But you're still looking at a mess, and if you're going to go to all that trouble, you're better off doing this:
require File.expand_path(File.dirname(__FILE__)) + "twitter"
Of course the nicer thing is to do this:
expand_path(dirname(__FILE__)) + string
require File.here "twitter"
I think in reality
Fileis a module, but you get the idea. You see
File.expand_path(File.dirname(__FILE__))all over the place, and it's ugly as sin. You see
$:.unshift(File.dirname(__FILE__))now and then, and it's not just ugly, but also vulnerable to annoying breakage. Any time you're faced with a choice between ugly shit and ugly shit that doesn't even work, the only sane response is to say "fuck no" and rewrite the rules so you're not faced with that kind of dilemma again.