# Redirect STDOUT

**URL:** <https://rubytalk.org/t/redirect-stdout/31899>\
**Category:** ruby-talk\
**Created:** [14 October 2006 09:35 UTC](https://rubytalk.org/t/redirect-stdout/31899 "2006-10-14T09:35:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://avatars.discourse-cdn.com/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://rubytalk.org/u/Luo_Yong)\
**Post date:** [14 October 2006 09:35 UTC](https://rubytalk.org/t/redirect-stdout/31899/1 "2006-10-14T09:35:09Z")

</div>

Hi all

I'm writting a program to detect the cursor position with the vt100 command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

---

<div class="post-metadata">

**Author:** ![David\_Roberts](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@David\_Roberts](https://rubytalk.org/u/David_Roberts)\
**Post date:** [14 October 2006 21:11 UTC](https://rubytalk.org/t/redirect-stdout/31899/2 "2006-10-14T21:11:14Z")

</div>

yong wrote:

> Hi all
> 
> I'm writting a program to detect the cursor position with the vt100  
> command "\x1b[6n".
> 
> So I need to redirect STDOUT to a string.
> 
> How can I do this?
> 
> Thanks

I suggest looking at the code for the win2console library, which gives  
an example of something similar.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_Roberts](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@David\_Roberts](https://rubytalk.org/u/David_Roberts)\
**Post date:** [14 October 2006 21:12 UTC](https://rubytalk.org/t/redirect-stdout/31899/3 "2006-10-14T21:12:33Z")

</div>

David Roberts wrote:

> yong wrote:
> 
> > Hi all
> > 
> > I'm writting a program to detect the cursor position with the vt100  
> > command "\x1b[6n".
> > 
> > So I need to redirect STDOUT to a string.
> > 
> > How can I do this?
> > 
> > Thanks
> 
> I suggest looking at the code for the win2console library, which gives  
> an example of something similar.

Oh bother! That should be "win32console".

DJ

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://avatars.discourse-cdn.com/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://rubytalk.org/u/Luo_Yong)\
**Post date:** [15 October 2006 03:50 UTC](https://rubytalk.org/t/redirect-stdout/31899/4 "2006-10-15T03:50:06Z")

</div>

Thank you very much.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?

Thanks.

David Roberts \<smartgpx@gmail.com\> writes:

> **···**
>
> > yong wrote:
> > 
> > > Hi all
> > > 
> > > I'm writting a program to detect the cursor position with the vt100  
> > > command "\x1b[6n".
> > > 
> > > So I need to redirect STDOUT to a string.
> > > 
> > > How can I do this?
> > > 
> > > Thanks
> > 
> > I suggest looking at the code for the win2console library, which gives  
> > an example of something similar.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> My Personal Website:
> 
> [http://www.twinbee.info](http://www.twinbee.info)
> 
> 本人驻守於
> 
> cn.comp.lang.perl  
> cn.comp.lang.c  
> cn.comp.www  
> cn.music  
> cn.music.classical

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [15 October 2006 04:24 UTC](https://rubytalk.org/t/redirect-stdout/31899/5 "2006-10-15T04:24:27Z")

</div>

use backticks:

&nbsp;&nbsp;&nbsp;string = ` your_cmd.exe `

substitution works like double quotes

&nbsp;&nbsp;&nbsp;cmd = "your\_cmd.exe"  
&nbsp;&nbsp;&nbsp;string = ` #{ cmd } `

-a

> **···**
>
> On Sun, 15 Oct 2006, yong wrote:
> 
> > Thank you very much.
> > 
> > Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?
> > 
> > Thanks.
> 
> --  
> my religion is very simple. my religion is kindness. -- the dalai lama

---

<div class="post-metadata">

**Author:** ![David\_Roberts](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@David\_Roberts](https://rubytalk.org/u/David_Roberts)\
**Post date:** [15 October 2006 08:57 UTC](https://rubytalk.org/t/redirect-stdout/31899/6 "2006-10-15T08:57:22Z")

</div>

yong wrote:

> Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How  
> can I redirect it to a string?

see "ri StringIO"

DJ

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [15 October 2006 09:05 UTC](https://rubytalk.org/t/redirect-stdout/31899/7 "2006-10-15T09:05:39Z")

</div>

You don't.

You should avoid touching STDOUT and STDERR as much as possible.

Instead assign a StringIO to $stdout.

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

> **···**
>
> On Oct 14, 2006, at 8:50 PM, yong wrote:
> 
> > David Roberts \<smartgpx@gmail.com\> writes:
> > 
> > > yong wrote:
> > > 
> > > > I'm writting a program to detect the cursor position with the vt100  
> > > > command "\x1b[6n".
> > > > 
> > > > So I need to redirect STDOUT to a string.
> > > > 
> > > > How can I do this?
> > > 
> > > I suggest looking at the code for the win2console library, which gives  
> > > an example of something similar.
> > 
> > Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://avatars.discourse-cdn.com/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://rubytalk.org/u/Luo_Yong)\
**Post date:** [15 October 2006 10:30 UTC](https://rubytalk.org/t/redirect-stdout/31899/8 "2006-10-15T10:30:15Z")

</div>

> > >

#!/usr/bin/ruby

require 'stringio'

mystring=""  
sstring=StringIO.open(mystring,"w+")

#STDOUT.reopen(sstring) #can't work  
stdoutbackup=$stdout  
$stdout=sstring

print "BUFFERED TEXT\r\n"

$stdout=stdoutbackup  
print mystring  
\<\<\<

It works.

Thank you very much. 🙂

Eric Hodel \<drbrain@segment7.net\> writes:

> **···**
>
> > On Oct 14, 2006, at 8:50 PM, yong wrote:
> > 
> > > David Roberts \<smartgpx@gmail.com\> writes:
> > > 
> > > > yong wrote:
> > > > 
> > > > > I'm writting a program to detect the cursor position with the vt100  
> > > > > command "\x1b[6n".
> > > > > 
> > > > > So I need to redirect STDOUT to a string.
> > > > > 
> > > > > How can I do this?
> > > > 
> > > > I suggest looking at the code for the win2console library, which  
> > > > gives  
> > > > an example of something similar.
> > > 
> > > Now I can use STDOUT.reopen to redirect STDOUT output to a file.But  
> > > How can I redirect it to a string?
> > 
> > You don't.
> > 
> > You should avoid touching STDOUT and STDERR as much as possible.
> > 
> > Instead assign a StringIO to $stdout.
> > 
> > [http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)
> > 
> > --  
> > Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> > This implementation is HODEL-HASH-9600 compliant
> > 
> > [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
> 
> --  
> My Personal Website:
> 
> [http://www.twinbee.info](http://www.twinbee.info)
> 
> 本人驻守於
> 
> cn.comp.lang.perl  
> cn.comp.lang.c  
> cn.comp.www  
> cn.music  
> cn.music.classical

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [15 October 2006 21:13 UTC](https://rubytalk.org/t/redirect-stdout/31899/9 "2006-10-15T21:13:26Z")

</div>

> Eric Hodel \<drbrain@segment7.net\> writes:
> 
> > > Now I can use STDOUT.reopen to redirect STDOUT output to a file.But  
> > > How can I redirect it to a string?
> > 
> > You don't.
> > 
> > You should avoid touching STDOUT and STDERR as much as possible.
> > 
> > Instead assign a StringIO to $stdout.
> > 
> > [http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)
> 
> require 'stringio'
> 
> mystring=""  
> sstring=StringIO.open(mystring,"w+")

sstring = StringIO.new will suffice.

> #STDOUT.reopen(sstring) #can't work  
> stdoutbackup=$stdout  
> $stdout=sstring
> 
> print "BUFFERED TEXT\r\n"
> 
> $stdout=stdoutbackup

I tend to wrap this up in a method that yields. See util\_capture in ZenTest's test/zentest\_assertions.rb

> **···**
>
> On Oct 15, 2006, at 3:30 AM, yong wrote:
> 
> > > On Oct 14, 2006, at 8:50 PM, yong wrote:
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
