OK...here is the (real life) scenario of writing a Jabber client...

<WhatIsJabber from="http://www.jabber.org">
"The first application of Jabber technology is an instant messaging system focused on privacy, security, ease of use, access from anywhere using any device, and interoperability with IM, phone, and web-based services. The result? Jabber is quickly becoming a standard component of Internet infrastructure."
...and...
"Jabber is fully based on XML, so it provides an extensible architecture for creating the next generation of services and applications on the Internet. The benefits of using Jabber include presence management, transparent interoperability, and real-time routing of structured information."
</WhatIsJabber>

So to communicate with a Jabber server you open a TCP socket and send/receive XML.

To parse the incoming XML stream fromt the Jabber server I used XMLParser (based on Expat).  I get the input stream from the socket and call @parser.parse(stream) {with a block}  The problem is the #parse method of XMLParser calls the #gets method on the stream passed into it.  #gets allows for an optional param indicating what to break the lines on.  Now, this defaults to "$/" which defaults to Carriage Return.  Well...because the Jabber protocol is a communications protocol, it does not insert CR between XML tags!  So when the parser called #gets it would never encounter a CR so would never parse!  I needed to _mold_ the stream object's #gets function into something that would work for the parser.  So I overrode the #gets method on the stream _object_ to call its "super" with #gets(">") which is the close tag for XML.

Parsing happens...problem solved (PS...I could have changed the value of $/ but I hate mucking with global variables...especially in a multithreaded app ;)

snip of code from my jabber stuff:

...
class JabberParser
  attr_reader :started
  def initialize(stream, listener)
    @listener = listener
    @stream = stream
    def @stream.gets #
      super(">")     # <= Here is where I overrode the gets method to give
    end              #    me the desired behavior.
  end
  def parse
    parser = XMLParser.new("UTF-8")
    begin
      parser.parse(@stream) do |type, name, data|
...

> -----Original Message-----
> From: Harry Ohlsen [mailto:harryo / zip.com.au]
> Sent: Sunday, December 09, 2001 9:39 PM
> To: ruby-talk ML; undisclosed-recipients:
> Subject: [ruby-talk:28054] Another question
> 
> 
> Does anyone have a good example of when one would want to redefine a
> method (or define a new one) for a single object?
> 
> Ie, something like
> 
>    a = [1, 2, 3]
> 
>    def a.fred
>       puts "Hi; I'm Fred!"
>    end
> 
> but that someone hearing about Ruby for the first time wouldn't think was
> really contrived.  It would need to be fairly simple, if that's possible.