[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: RE: Is better to subclass or to add methods to an existing class?
From: "Volkmann, Mark" <Mark.Volkmann () AGEDWARDS ! com>
Date: 2002-09-19 12:44:05
[Download RAW message or body]
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.
************************************************************************************
[Attachment #3 (text/html)]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: Is better to subclass or to add methods to an existing class?</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>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.</FONT></P>
<P><FONT SIZE=2>I'm sure some see that is a dangerous feature of Ruby, but it can be \
useful in the right context.</FONT> </P>
<P><FONT SIZE=2>> On Thu, 19 Sep 2002, Vincent Foley wrote:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> > I was discussing with a (Python) friend last night. \
I told </FONT> <BR><FONT SIZE=2>> him that one</FONT>
<BR><FONT SIZE=2>> > thing I liked better about Ruby than Python was that you \
could add</FONT> <BR><FONT SIZE=2>> > methods to already existing \
methods. For instance, if I </FONT> <BR><FONT SIZE=2>> wanted to add \
a</FONT> <BR><FONT SIZE=2>> > rot13 method to the String class, all I have to \
do is this:</FONT> <BR><FONT SIZE=2>> ></FONT>
<BR><FONT SIZE=2>> > [code]</FONT>
<BR><FONT SIZE=2>> > class String</FONT>
<BR><FONT SIZE=2>> > def rot13</FONT>
<BR><FONT SIZE=2>> > tr("A-Za-z", \
"N-ZA-Mn-za-m")</FONT> <BR><FONT SIZE=2>> > end</FONT>
<BR><FONT SIZE=2>> > end</FONT>
<BR><FONT SIZE=2>> ></FONT>
<BR><FONT SIZE=2>> > "foobar".rot13</FONT>
<BR><FONT SIZE=2>> > [/code]</FONT>
<BR><FONT SIZE=2>> ></FONT>
<BR><FONT SIZE=2>> > But my friend told me that Python didn't have that because \
</FONT> <BR><FONT SIZE=2>> it was not a</FONT>
<BR><FONT SIZE=2>> > good thing and it was not the proper way to do it. \
He said that the</FONT> <BR><FONT SIZE=2>> > true way of doing it, is to \
subclass (since Python 2.2 can </FONT> <BR><FONT SIZE=2>> now subclass</FONT>
<BR><FONT SIZE=2>> > builtin types) the base class:</FONT>
</P>
<CODE><FONT SIZE=3><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>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic