Monkey vs. Donkey: Fight!
“I’ve seen things you people wouldn’t believe.” - Roy Batty
One of the things I thoroughly enjoy about Ruby are the linguistic gymnastics it allows developers to pull off. In my brief time with Ruby I have laughed at the cleverness and audacity of more code than any other language I can recall. Not a “ha ha” laugh of mockery or dismay, but one of sheer delight in the truly weird shit that other people tweak out of it.
Rails’ gymnastics with method_missing(), alias() chaining, to_proc() weirdness - clearly that way lies madness! …or does it?
Whether I think the use of those techniques is a good idea or not, or whether I choose to follow down those obscure and dark meta-programming pathways or not is a personal choice of really no consequence at all (unless, perhaps, you have to maintain my code) but that they can be done at all is of significant consequence to all developers. The existence of these features is sufficient to stretch my mind and affect my thinking about how programming languages can and should and do and might work and for that I celebrate them.
It is no small understatement to say that in fifteen years as a developer no other language has instilled in me the joy of simply poking and prodding at it, and following along as others smarter than I poke harder and prod deeper at it, than Ruby has.
“Liberty means responsibility. That is why most men dread it.” - George Bernard Shaw
Of course the black sheep of Ruby meta-programming is so-called “monkey-patching”, a most excellent term if ever there was one.
Oddly enough, when I first heard the term, in the context of Rails, I didn’t fully understand the implications at the time and took monkey-patching to mean simply the dynamic addition of functionality to a class (ie: “adding stuff”) whereas I now correctly understand it to mean both that and the dynamic modification of existing functionality in a class (ie: “screwing with stuff”). Unfortunately for me my first understanding has imprinted itself so deeply to the point where I intrinsically think of monkey-patching as a Good Thing™ and often get caught unawares when someone rips off on a rant against it (that Good Thing is not a statement of absolutism, kindly calm yourself down before firing off a “you’re an idiot because!” comment, those who tend to do so).
Not that I don’t absolutely anti-disagree that screwing with stuff is not a good idea. I am vehemently in the “don’t fuck with established, documented functionality” camp. If your clever gem over-rides my Ruby’s Object#eql? method I’ll cheerfully join the mob and bring my torch (the burny kind, not the battery-operated kind, for my British readers).
But I really enjoy additive monkey-patching. I like that I don’t have to wait for the language designers to decide to incorporate new functionality. I like that I can install a gem and get Integer#even? intrinsic to all my numbers (yes I am aware of how trivial the code to implement that functionality is, thank you very much). I like that sometimes I have to lean back in my chair and think “module or monkey-patch?” before hacking away. It’s a good dilemma to have, this thinking about the fundamental implementation, and implication, of language functionality.
Is monkey-patching over-used? Abused? I don’t know; I honestly don’t have an opinion on that. My experience says less is better so if I had to pick a side I’d tend towards the “monkey-patch as last resort” camp but sometimes it seems to make perfect sense.
But only as an additive.
To deal with this schism in my thinking about monkey-patching I’ve come up with another term for code that screws with established functionality: donkey-patching (the poker plays in the room should get the reference immediately).
If I may steal from the Wikipedia and modify their monkey-patch definition a bit:
A donkey patch (also spelled donkey-patch, DonkeyPatch) is a way to extend or modify established, standard core runtime code without altering the original source code for dynamic languages (e.g. Smalltalk, Javascript, Ruby, Perl, and Python).
A common source of unintended consequence, teeth-nashing, anger and frustration.
A bad idea.
Friends don’t let friends donkey-patch.

anonymous responded on 27 Feb 2008 at 3:04 pm #
“I don’t absolutely anti-disagree”… jeesus man… not not not agree… was that really necessarily? stop donkey-patching your english!
chris responded on 27 Feb 2008 at 3:20 pm #
I’m impressed! I certainly didn’t expect that the first comment posted would get that joke. Well done sir.