# Making my own output stream

**URL:** <https://rubytalk.org/t/making-my-own-output-stream/8186>\
**Category:** ruby-talk\
**Created:** [22 September 2003 01:28 UTC](https://rubytalk.org/t/making-my-own-output-stream/8186 "2003-09-22T01:28:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [22 September 2003 01:28 UTC](https://rubytalk.org/t/making-my-own-output-stream/8186/1 "2003-09-22T01:28:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thomas\_Sondergaard](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@Thomas\_Sondergaard](https://rubytalk.org/u/Thomas_Sondergaard)\
**Post date:** [22 September 2003 01:28 UTC](https://rubytalk.org/t/making-my-own-output-stream/8186/2 "2003-09-22T01:28:14Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [22 September 2003 02:34 UTC](https://rubytalk.org/t/making-my-own-output-stream/8186/3 "2003-09-22T02:34:24Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [23 September 2003 16:34 UTC](https://rubytalk.org/t/making-my-own-output-stream/8186/4 "2003-09-23T16:34:14Z")

</div>

Thanks, that answers that question. Ah, well. 🙂

-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

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [23 September 2003 16:34 UTC](https://rubytalk.org/t/making-my-own-output-stream/8186/5 "2003-09-23T16:34:17Z")

</div>

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?
