Making my own output stream

This is probably a dumb question, but:
if I want to make a class such that an instance of it
can be assigned to $stdout without breaking anything,
what does it need to implement? Can I just define a #write
method and include some mixin to handle the rest?

-Mark

This is probably a dumb question, but:
if I want to make a class such that an instance of it
can be assigned to $stdout without breaking anything,
what does it need to implement? Can I just define a #write
method and include some mixin to handle the rest?

Unfortunately not. Implementing a new IO is not as trivial as it should be.
For rubyzip I have implemented an AbstractOutputStream module that
implements all the “output” methods of IO

class TestOutputStream
include AbstractOutputStream
attr_accessor :buffer

    def initialize
        @buffer = ""
    end

    def << (data)
          @buffer << data
          self
    end

end

You could try it and see if it works.

Cheers,

Thomas

This is probably a dumb question, but:
if I want to make a class such that an instance of it
can be assigned to $stdout without breaking anything,
what does it need to implement? Can I just define a #write
method and include some mixin to handle the rest?

-Mark

Would inheriting from StringIO work?

class MarksIO < StringIO

end

Gavin

Thanks, that answers that question. Ah, well. :slight_smile:

-Mark

···

On Mon, Sep 22, 2003 at 03:23:11AM +0200, Thomas Sondergaard wrote:

Unfortunately not. Implementing a new IO is not as trivial as it should be.
For rubyzip I have implemented an AbstractOutputStream module that
implements all the “output” methods of IO

It would, but I’d still have to overload just about every method.
However, encapsulating a StringIO works, because then I can use
method_missing to delegate calls to the enclosed StringIO, without
specifically writing near-duplicate code for every method.

Thanks for the tip!

-Mark

···

On Mon, Sep 22, 2003 at 11:34:24AM +0900, Gavin Sinclair wrote:

Would inheriting from StringIO work?