Re:Refinements in Ruby — Monkey patching for friendly monkeys
by jerf on Sunday November 28, 2010 @03:50PM
(#1948254)
The badness of monkeypatching is its ability to make non-local changes that stomp on other code. Monkeypatches aren't bad in a general sense. Take away the stomping behavior and you've taken away the badness.
(They might still be "dangerous" but you've moved them out of the "suicidal" territory I believe non-scoped monkeypatches live in.)
Incidentally, Perl has been able to do this for a long time. I've gotten a lot of mileage out of:
local *{'Some::Module::method'} = sub { ... };
my $thing = new Some::Module();
...
$thing->method(...);
...
It's great for testing, great for modifying the way libraries work locally without globally modifying them (in my case, hacking a bit on code shared by many projects in such a way that I can't really add this thing I really need), and so on. A great addition to Ruby that goes a long way towards mitigating some of my fundamental complaints about the language.
(They might still be "dangerous" but you've moved them out of the "suicidal" territory I believe non-scoped monkeypatches live in.)
Incidentally, Perl has been able to do this for a long time. I've gotten a lot of mileage out of:
It's great for testing, great for modifying the way libraries work locally without globally modifying them (in my case, hacking a bit on code shared by many projects in such a way that I can't really add this thing I really need), and so on. A great addition to Ruby that goes a long way towards mitigating some of my fundamental complaints about the language.