How and why I overrode Kernel.autoload
Recently, I wrote transcoder as a means of parsing common web formats (
rss/atom anyone?) without being too concerned about what a web service or API was returning to you.
I pushed out 0.1.0 hastily and went for sake, glorious, comfortable sake. I knew however, that something wasn’t quite right.
When transcoder was required' it would bring in all of its dependencies, so if you were only hoping to parse a
yaml file, you got everything: json, xml and rss. Not good, too much.
Autoload is a Ruby kernel method, you give it a constant and a filepath, when the constant is found to be undefined, it attempts to load the filepath. Fantastic right? Well, sadly. No.
Autoload will only load from your
$LOAD_PATH, so I decided to roll up my sleeves.
Now, I’m sure that overriding
Object is pretty much a no-no in nearly every circumstance, but
Kernel as well? Ludacris!
No matter how bat-shit-crazy you might think this hack is, its actually very awesome.
I wrote this post full-well knowing that this was a pretty dirty solution – certainly not one that you would want in library code.
Where do you turn? To Evan Phoenix, thats who.
They’ve probably thought about or wrestled with similar design issues or requirements while implementing the core ruby language.
Evan explained a design solution, and I implemented it. Hopefully I didn’t trample too much over this. A big thanks is due to Evan.