From: Avdi Grimm Date: 2012-06-20T09:23:40+09:00 Subject: Re: Looking for a better way to add a method to a core class than monkey patching --0016e6dd8983e2472004c2dc6882 Content-Type: text/plain; charset=ISO-8859-1 Please do not monkey patch core libraries in gems, unless the *purpose* of your library is to add functionality to core, eg a library of extensions like activesupport. Yes, this goes for additions as well as overrides. I cannot tell you how many times I've seen a "harmless addition" mess things up in confusing and hard to debug ways. What method do you want to add to strings? -- Avdi Grimm http://avdi.org On Jun 19, 2012 1:35 PM, "Iain Barnett" wrote: > Hi all, > > I'd like to add a method to the String class for a gem I'm making. > Obviously, I could just open it up and pop the method in, but I'd like to > know what is the state-of-the-art for adding a method. Do I get anything > better from putting the method in a module and then opening up the class > and including it? I can't see why but I'd like to know. > > I don't wish to use a new class that inherits from String because I > understand that there's a gotcha with String due to something done for > performance reasons (though I can't remember the exact details I remember > the warning!), and there's obvious good sides to the method being directly > in String that far outweigh the inheritance angle. > > Any thoughts or suggestions would be appreciated. > > Regards, > Iain > --0016e6dd8983e2472004c2dc6882 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

Please do not monkey patch core libraries in gems, unless the *purpose* = of your library is to add functionality to core, eg a library of extensions= like activesupport. Yes, this goes for additions as well as overrides. I c= annot tell you how many times I've seen a "harmless addition"= mess things up in confusing and hard to debug ways.

What method do you want to add to strings?

--
Avdi Grimm
http://avdi.org

On Jun 19, 2012 1:35 PM, "Iain Barnett"= ; <iainspeed@gmail.com> wr= ote:
Hi all,

I'd like to add a method to the String class for a gem I'm making. = Obviously, I could just open it up and pop the method in, but I'd like = to know what is the state-of-the-art for adding a method. Do I get anything= better from putting the method in a module and then opening up the class a= nd including it? I can't see why but I'd like to know.

I don't wish to use a new class that inherits from String because I und= erstand that there's a gotcha with String due to something done for per= formance reasons (though I can't remember the exact details I remember = the warning!), and there's obvious good sides to the method being direc= tly in String that far outweigh the inheritance angle.

Any thoughts or suggestions would be appreciated.

Regards,
Iain
--0016e6dd8983e2472004c2dc6882--