[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Making my own output stream

Mark J. Reed

9/22/2003 1:09:00 AM

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
4 Answers

Thomas Sondergaard

9/22/2003 1:23:00 AM

0

> 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


Gavin Sinclair

9/22/2003 2:34:00 AM

0

> 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



Mark J. Reed

9/23/2003 4:32:00 PM

0

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

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

-Mark

Mark J. Reed

9/23/2003 4:33:00 PM

0

On Mon, Sep 22, 2003 at 11:34:24AM +0900, Gavin Sinclair wrote:
> Would inheriting from StringIO work?

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