Among the features backported into 1.8.7 from Ruby 1.9 is a new#chars
attribute. Unfortunately, it is incompatible with the Rails 2.0 implementation of#chars
.
— Avdi Grimm, The Trifecta of FAIL
It's a bad idea to remodel a house you don't own. Maybe the word "monkeysquatting" is more descriptive (if not more visceral).
Speaking as one who has added methods to UNIVERSAL and, on at least one occasion, set @UNIVERSAL::ISA, I'd say there's nothing wrong with monkeying with classes you don't 'own'. What always gave me the heeby jeebies about the way Rails does it is that they chose names that were obvious hostages to fortune.
When I was hacking on Pixie and needed to add methods to UNIVERSAL, everything that went in got a px_ prefix and should probably have had a pixie_ one. (Actually, now I think about it, we used to take advantage of the way you can override Perl's method dispatch by doing $obj->px::pickle, so we could have arranged to use totally opaque names on UNIVERSAL itself:
package px;
{
my $pickle = "pixie_" . SHA1(random() + 'pixie');
sub pickle {
my $self = shift;
$self->can('pixie_pickle')
? $self->pixie_pickle(@_)
: $self->$pickle(@_);
}
}
I just tried writing something similar in Ruby and it can't be done (for values of "can't be done" that involve no more than 10 minutes thought), you're left having to use a hopefully unique prefix for the methods you add.
But you're right; that particular post left me gasping. I particularly liked the "It's the fault of those dashed Japanese people for not understanding that if you change the sub sub version number, you shouldn't go changing the interface of your core classes!" protest. It's not like it hasn't happened with pretty much every sub release of Ruby so far. You'd think he'd be used to it by now. You might, further, think that the maintainers of Rails might have learned by now...
Sometimes, I miss perl programming a great deal.
Re:That one's been waiting to happen for a while
pdcawley on 2008-08-08T05:37:06
Yes, the algorithm for choosing a safe name is, ah, less than wonderful. Should at probably be SHA1('pixie:pickle'), assuming we don't mind outsiders being able to guess the opaque method's name.
Re:That one's been waiting to happen for a while
chromatic on 2008-08-08T06:37:19
I'd say there's nothing wrong with monkeying with classes you don't 'own'.
... until Daddy comes home, anyway.
My amusement comes from the idea that Rails could have done the right thing (incidentally avoiding Primitive Obsession as well as monkeysquatting) by using its own String subclass. It's opinionated software. You use its own APIs throughout your system. This is cheap insurance -- and think how easy it would be to fix HTML escaping in one place, for all time.
I particularly liked the "It's the fault of those dashed Japanese people for not understanding that if you change the sub sub version number, you shouldn't go changing the interface of your core classes!" protest.
That was rich, especially one sentence after explaining that not all projects follow that numerology.
You might, further, think that the maintainers of Rails might have learned by now...
Metaprogramming needs a Gödel.
Re:New monkey-squatting name?
chromatic on 2008-08-09T00:46:47
Is there a better term to describe pretending to own a namespace and getting in trouble when your ideas of ownership conflict with that of the real owners?
Re:New monkey-squatting name?
mr_bean on 2008-08-10T14:30:38
I like squatting as a name.
It is more self-explanatory than monkey-patching,
which name apparently is indirectly derived from guerrilla-
patching, through gorilla-patching.I wonder if that's true or not.