# I want to redirect stderr to StringIO

**URL:** <https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374>\
**Category:** Ruby\
**Created:** [May 20, 2009, 6:31am UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374 "2009-05-20T06:31:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 20, 2009, 6:31am UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/1 "2009-05-20T06:31:30Z")

</div>

> > IO.new(2).reopen(StringIO.new)  
> > TypeError: can’t convert StringIO into String  
> > from (irb):1:in `reopen’  
> > from (irb):1
> 
> > IO.new(2).reopen(IO.new(1))  
> > =\> #IO:0x2a81630

StringIO also do as IO, but IO#reopen fails.

I want to redirect stderr to StringIO, but it doesn’t work.

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 20, 2009, 6:39am UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/2 "2009-05-20T06:39:42Z")

</div>

On May 19, 2009, at 21:30, ErMaker wrote:

> > > IO.new(2).reopen(StringIO.new)  
> > > TypeError: can’t convert StringIO into String  
> > > from (irb):1:in `reopen’  
> > > from (irb):1  
> > > IO.new(2).reopen(IO.new(1))  
> > > =\> #IO:0x2a81630
> 
> StringIO also do as IO, but IO#reopen fails.
> 
> I want to redirect stderr to StringIO, but it doesn’t work.

Always use reopen as a last resort.

[http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 20, 2009, 8:15am UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/3 "2009-05-20T08:15:41Z")

</div>

2009/5/20 Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > =\> #IO:0x2a81630
> > 
> > StringIO also do as IO, but IO#reopen fails.
> > 
> > I want to redirect stderr to StringIO, but it doesn’t work.
> 
> Always use reopen as a last resort.
> 
> [http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)

I am not sure I agree. Actually, if you want the redirection to be  
permanent for sub processes you have to use $stdout.reopen. And this  
is not a “last resort” but the proper solution.

Btw, in 1.8 there is also $defout - I believe \_that- is the stream  
used by Kernel#puts and the like in those versions:

08:13:00 ~$ ruby -r stringio -e ‘puts 1; $defout=StringIO.new; puts 2’  
1  
-e:1: warning: $defout is obsolete; use $stdout instead  
08:13:30 ~$

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 20, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/4 "2009-05-20T21:02:30Z")

</div>

On May 19, 2009, at 23:14, Robert K. wrote:

> > >
> 
> I am not sure I agree. Actually, if you want the redirection to be  
> permanent for sub processes you have to use $stdout.reopen. And this  
> is not a “last resort” but the proper solution.

Most of the time people capture IO from sub processes via Kernel#` or  
IO::popen instead of handling the sub process themselves. Ruby’s nice  
features make use of #reopen an exceptional circumstance, but there  
are times where it’s appropriate.

> Btw, in 1.8 there is also $defout - I believe \_that- is the stream  
> used by Kernel#puts and the like in those versions:
> 
> 08:13:00 ~$ ruby -r stringio -e ‘puts 1; $defout=StringIO.new; puts 2’  
> 1  
> -e:1: warning: $defout is obsolete; use $stdout instead

This warning says otherwise. They happen to be the same object though:

$ ruby -e ‘p $stdout.object\_id, $defout.object\_id’  
97260  
97260

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 22, 2009, 6:28am UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/5 "2009-05-22T06:28:11Z")

</div>

On 20.05.2009 21:01, Eric H. wrote:

> On May 19, 2009, at 23:14, Robert K. wrote:
> 
> > 2009/5/20 Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > [http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)  
> > > I am not sure I agree. Actually, if you want the redirection to be  
> > > permanent for sub processes you have to use $stdout.reopen. And this  
> > > is not a “last resort” but the proper solution.
> 
> Most of the time people capture IO from sub processes via Kernel#` or  
> IO::popen instead of handling the sub process themselves. Ruby’s nice  
> features make use of #reopen an exceptional circumstance, but there  
> are times where it’s appropriate.

You do not make that point on the blog entry which provoked my remark.  
(I wanted to place it there but comments are closed.)

> > Btw, in 1.8 there is also $defout - I believe \_that- is the stream  
> > used by Kernel#puts and the like in those versions:
> > 
> > 08:13:00 ~$ ruby -r stringio -e ‘puts 1; $defout=StringIO.new; puts 2’  
> > 1  
> > -e:1: warning: $defout is obsolete; use $stdout instead
> 
> This warning says otherwise.

Please do not let yourself be distracted by the warning. The crucial  
point is that after $defout has been reassigned the “2” does not appear  
any more on the screen =\> this is the object which is used by #puts.

> They happen to be the same object though:
> 
> $ ruby -e ‘p $stdout.object\_id, $defout.object\_id’  
> 97260  
> 97260

Yes - until you reassign any of them. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 22, 2009, 9:35am UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/6 "2009-05-22T09:35:07Z")

</div>

Robert K. wrote:

> Please do not let yourself be distracted by the warning. The crucial  
> point is that after $defout has been reassigned the “2” does not appear  
> any more on the screen =\> this is the object which is used by #puts.

Just do what the warning says, and use $stdout instead of $defout:

$ ruby -r stringio -e ‘puts 1; $stdout=StringIO.new; puts 2’  
1  
$

However this doesn’t help the OP who wanted to redirect STDERR. You can  
redirect $stderr, but lots of code writes to STDERR instead of $stderr.

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 20, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/7 "2009-05-20T12:29:47Z")

</div>

ErMaker wrote:

> > > IO.new(2).reopen(StringIO.new)  
> > > TypeError: can’t convert StringIO into String  
> > > from (irb):1:in `reopen’  
> > > from (irb):1  
> > > IO.new(2).reopen(IO.new(1))  
> > > =\> #IO:0x2a81630
> 
> StringIO also do as IO, but IO#reopen fails.
> 
> I want to redirect stderr to StringIO, but it doesn’t work.

The problem is that a StringIO cannot exist in the O/S’s file descriptor  
table. STDERR.reopen(…) at the low level does a dup() or dup2() to  
copy one file descriptor to another.

You have two options:

(1) $stderr = StringIO.new

Then any program which writes to $stderr will be fine. But anything  
which writes to STDERR will still go to file descriptor 2 (that is, your  
process’ stderr file)

(2) reopen STDERR with something which exists in the O/S file descriptor  
table: e.g. a file or a pipe.

So for example, you can fork and connect a pipe to the child process’  
STDERR, and then in the parent have a thread which reads from this and  
copies all data read into a StringIO object. Anything written to STDERR  
in the child process will be collected into the StringIO via the pipe.

See open3.rb in the standard library for an example of how to do this.

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 22, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/8 "2009-05-22T17:59:20Z")

</div>

On May 21, 2009, at 21:27, Robert K. wrote:

> Please do not let yourself be distracted by the warning. The  
> crucial point is that after $defout has been reassigned the “2” does  
> not appear any more on the screen =\> this is the object which is  
> used by #puts.

Other than the warning, the behavior is the same with $stdout and  
$defout as they are the same variable.

> > They happen to be the same object though:  
> > $ ruby -e ‘p $stdout.object\_id, $defout.object\_id’  
> > 97260  
> > 97260
> 
> Yes - until you reassign any of them. 🙂

If you reassign either of them they’re still the same object:

$ ruby -r stringio -e ‘$stdout=StringIO.new; $stderr.puts [$stdout,  
$defout].inspect’  
[#StringIO:0x28a50, #StringIO:0x28a50]

From io.c:

rb\_define\_hooked\_variable(“$stdout”, &rb\_stdout, 0, stdout\_setter);  
[…]  
rb\_define\_hooked\_variable(“$defout”, &rb\_stdout, 0, defout\_setter);

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 22, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/9 "2009-05-22T18:01:41Z")

</div>

On May 22, 2009, at 00:35, Brian C. wrote:

> $
> 
> However this doesn’t help the OP who wanted to redirect STDERR. You  
> can  
> redirect $stderr, but lots of code writes to STDERR instead of  
> $stderr.

Per the URL in my earlier message, this software is wrong and bugs  
should be filed.

---

<div class="post-metadata">

**Author:** ![ErMaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@ErMaker](https://www.ruby-forum.com/u/ErMaker)\
**Post date:** [May 24, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/i-want-to-redirect-stderr-to-stringio/168374/10 "2009-05-24T17:30:54Z")

</div>

On 22.05.2009 17:57, Eric H. wrote:

> On May 21, 2009, at 21:27, Robert K. wrote:
> 
> > On 20.05.2009 21:01, Eric H. wrote:

> [#StringIO:0x28a50, #StringIO:0x28a50]  
> Oh, I wasn’t aware of this. Thank you for the education!

Kind regards

robert
