Hello --

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:

I tend to go for the adding methods approach, but there's always the
issue of name conflicts among different libraries....

For what it's worth, personally I wouldn't subclass String to get
rot13 functionality, even if I were opposed to create String#rot13.
I'd rather do something like:

  class Rot13
    def self.[](str)
      str.tr("A-Za-z", "N-ZA-Mn-za-m")
    end
  end

  Rot13["abc"]   # => "nop"


David

-- 
David Alan Black                      | Register for RubyConf 2002!
home: dblack / candle.superlink.net     | November 1-3
work: blackdav / shu.edu                | Seattle, WA, USA
Web:  http://pirate.shu.edu/~blackdav | http://www.rubyconf.com