From: "Volkmann, Mark" Date: 2002-09-19T21:44:05+09:00 Subject: RE: Is better to subclass or to add methods to an existing class? This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C25FDA.386A6040 Content-Type: text/plain; charset="iso-8859-1" I think I'd only inject a method into an existing class if I need to modify functionality of existing methods in code I couldn't change. For example, obviously many Ruby libraries using the String class. You can't, at least easily, modify all Ruby libraries to use a new subclass of String, but you can cause them to behave differently when they invoke a standard String method by redefining that method. I'm sure some see that is a dangerous feature of Ruby, but it can be useful in the right context. > On Thu, 19 Sep 2002, Vincent Foley wrote: > > > I was discussing with a (Python) friend last night. I told > him that one > > thing I liked better about Ruby than Python was that you could add > > methods to already existing methods. For instance, if I > wanted to add a > > rot13 method to the String class, all I have to do is this: > > > > [code] > > class String > > def rot13 > > tr("A-Za-z", "N-ZA-Mn-za-m") > > end > > end > > > > "foobar".rot13 > > [/code] > > > > But my friend told me that Python didn't have that because > it was not a > > good thing and it was not the proper way to do it. He said that the > > true way of doing it, is to subclass (since Python 2.2 can > now subclass > > builtin types) the base class: *********************************************************************************** WARNING: All e-mail sent to and from this address will be received or otherwise recorded by the A.G. Edwards corporate e-mail system and is subject to archival, monitoring or review by, and/or disclosure to, someone other than the recipient. ************************************************************************************ ------_=_NextPart_001_01C25FDA.386A6040 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: Is better to subclass or to add methods to an existing class?</T= ITLE> </HEAD> <BODY> <P><FONT SIZE=3D2>I think I'd only inject a method into an existing class i= f I need to modify functionality of existing methods in code I couldn't cha= nge.  For example, obviously many Ruby libraries using the String clas= s.  You can't, at least easily, modify all Ruby libraries to use a new= subclass of String, but you can cause them to behave differently when they= invoke a standard String method by redefining that method.</FONT></P> <P><FONT SIZE=3D2>I'm sure some see that is a dangerous feature of Ruby, bu= t it can be useful in the right context.</FONT> </P> <P><FONT SIZE=3D2>> On Thu, 19 Sep 2002, Vincent Foley wrote:</FONT> <BR><FONT SIZE=3D2>> </FONT> <BR><FONT SIZE=3D2>> > I was discussing with a (Python) friend last n= ight.  I told </FONT> <BR><FONT SIZE=3D2>> him that one</FONT> <BR><FONT SIZE=3D2>> > thing I liked better about Ruby than Python wa= s that you could add</FONT> <BR><FONT SIZE=3D2>> > methods to already existing methods.  For= instance, if I </FONT> <BR><FONT SIZE=3D2>> wanted to add a</FONT> <BR><FONT SIZE=3D2>> > rot13 method to the String class, all I have t= o do is this:</FONT> <BR><FONT SIZE=3D2>> ></FONT> <BR><FONT SIZE=3D2>> > [code]</FONT> <BR><FONT SIZE=3D2>> > class String</FONT> <BR><FONT SIZE=3D2>> >   def rot13</FONT> <BR><FONT SIZE=3D2>> >     tr("A-Za-z",= "N-ZA-Mn-za-m")</FONT> <BR><FONT SIZE=3D2>> >   end</FONT> <BR><FONT SIZE=3D2>> > end</FONT> <BR><FONT SIZE=3D2>> ></FONT> <BR><FONT SIZE=3D2>> > "foobar".rot13</FONT> <BR><FONT SIZE=3D2>> > [/code]</FONT> <BR><FONT SIZE=3D2>> ></FONT> <BR><FONT SIZE=3D2>> > But my friend told me that Python didn't have = that because </FONT> <BR><FONT SIZE=3D2>> it was not a</FONT> <BR><FONT SIZE=3D2>> > good thing and it was not the proper way to do= it.  He said that the</FONT> <BR><FONT SIZE=3D2>> > true way of doing it, is to subclass (since Py= thon 2.2 can </FONT> <BR><FONT SIZE=3D2>> now subclass</FONT> <BR><FONT SIZE=3D2>> > builtin types) the base class:</FONT> </P> <CODE><FONT SIZE=3D3><BR> <BR> ***************************************************************************= ********<BR> WARNING: All e-mail sent to and from this address will be received or<BR> otherwise recorded by the A.G. Edwards corporate e-mail system and is<BR> subject to archival, monitoring or review by, and/or disclosure to,<BR> someone other than the recipient.<BR> ***************************************************************************= *********<BR> </FONT></CODE></BODY> </HTML> ------_=_NextPart_001_01C25FDA.386A6040--