# Redirecting standard output

**URL:** <https://rubytalk.org/t/redirecting-standard-output/56574>\
**Category:** ruby-talk\
**Created:** [10 December 2009 14:48 UTC](https://rubytalk.org/t/redirecting-standard-output/56574 "2009-12-10T14:48:45Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [10 December 2009 14:48 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/1 "2009-12-10T14:48:45Z")

</div>

Omar Campos wrote:

> Hi all,
> 
> &nbsp;&nbsp;Is there a way to redirect the standard output, so that command like  
> puts write to a file instead of the console?

Using this as a way to shamelessly promote my 'desc\_method' gem... 🙂

> > $stdout.methods

=\> [:reopen, :print, ...]

> > require 'desc\_method'

=\> true

> > $stdout.desc\_method :reopen

sig: #\<Method: IO#reopen\> arity: -1  
appears to be a c method  
searching ri for IO#reopen...  
-------------------------------------------------------------- IO#reopen  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios.reopen(other\_IO) =\> ios  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios.reopen(path, mode\_str) =\> ios

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;From Ruby 1.9.1

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reassociates \_ios\_ with the I/O stream given in \_other\_IO\_ or to a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new stream opened on \_path\_. This may dynamically change the actual  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class of this stream.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f1 = File.new("testfile")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f2 = File.new("testfile")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f2.readlines[0] #=\> "This is line one\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f2.reopen(f1) #=\> #\<File:testfile\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f2.readlines[0] #=\> "This is line one\n"
> 
> (end ri)  
> =\> "#\<Method: IO#reopen\>"
> 
> > > $stdout.reopen("abc", "w")
> 
> Enjoy  
> -r  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Omar\_Campos](https://avatars.discourse-cdn.com/v4/letter/o/71c47a/32.png) [@Omar\_Campos](https://rubytalk.org/u/Omar_Campos)\
**Post date:** [11 December 2009 01:44 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/2 "2009-12-11T01:44:44Z")

</div>

Thanks, that's exactly what I was looking for! Nice gem by the way. I  
tried to "gem install" it, but I got a weird error message, don't  
remember what it was.

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

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [11 December 2009 02:35 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/3 "2009-12-11T02:35:29Z")

</div>

Omar Campos wrote:

> Thanks, that's exactly what I was looking for! Nice gem by the way. I  
> tried to "gem install" it, but I got a weird error message, don't  
> remember what it was.

No need for a gem.

$stdout = File.new '/path/to/file', 'w'  
puts 'foobar'  
$stdout = STDOUT  
puts 'foobar'

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

---

<div class="post-metadata">

**Author:** ![Luc\_Heinrich](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@Luc\_Heinrich](https://rubytalk.org/u/Luc_Heinrich)\
**Post date:** [11 December 2009 07:39 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/4 "2009-12-11T07:39:37Z")

</div>

Assignment to $stdout is deprecated (at least according to pickaxe), using reopen is the right way to do it:

$stdout.reopen(File.new("/path/to/file", "w"))

> **···**
>
> On 11 déc. 2009, at 03:35, Robert Gleeson wrote:
> 
> > $stdout = File.new '/path/to/file', 'w'  
> > puts 'foobar'  
> > $stdout = STDOUT  
> > puts 'foobar'
> 
> --  
> Luc Heinrich - luc@honk-honk.com

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 December 2009 10:35 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/5 "2009-12-11T10:35:29Z")

</div>

> Omar Campos wrote:
> 
> > Thanks, that's exactly what I was looking for! Nice gem by the way. I  
> > tried to "gem install" it, but I got a weird error message, don't  
> > remember what it was.
> 
> No need for a gem.

The gem was not needed for the redirection functionality but for  
getting at information about the method.

> $stdout = File.new '/path/to/file', 'w'  
> puts 'foobar'  
> $stdout = STDOUT  
> puts 'foobar'

This does only work for redirections of output for the particular  
process. It will fail in these cases

- sub processes started  
- all entities which remembered $stdout instance (e.g. a logger)

Your last solution is better (although you do not necessarily need a  
constant for that). Btw, apparently you don't even need the mode -  
that is inherited from the original. So you can do:

$ ruby19 -e '$stderr.reopen("log"); $stderr.puts("foo")' ; cat log  
foo  
$ ruby19 -e '$stdin.reopen("log"); p gets'  
"foo\n"  
$

Cheers

robert

> **···**
>
> 2009/12/11 Robert Gleeson \<rob@flowof.info\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [11 December 2009 07:53 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/6 "2009-12-11T07:53:44Z")

</div>

> Assignment to $stdout is deprecated (at least according to pickaxe),  
> using reopen is the right way to do it:
> 
> $stdout.reopen(File.new("/path/to/file", "w"))

Yup, you can use IO#reopen too. I don't know about being "deprecated".

$stdout.reopen will also redirect STDOUT to the location referenced in  
your call to #reopen. How can you redirect $stdout to its original  
location now?

The above solution ensures you can easily switch back.

- Rob

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

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [11 December 2009 13:49 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/7 "2009-12-11T13:49:14Z")

</div>

> The gem was not needed for the redirection functionality but for  
> getting at information about the method.

Yup, I completely missed that! I was tired, and misread the thread  
completely.

> > $stdout = File.new '/path/to/file', 'w'  
> > puts 'foobar'  
> > $stdout = STDOUT  
> > puts 'foobar'
> 
> This does only work for redirections of output for the particular  
> process. It will fail in these cases
> 
> - sub processes started

Hmm really? I thought a fork() would copy the memory from the parent  
process into the subprocess.  
Example:

&nbsp;&nbsp;$stdout = File.open 'foobar.txt', 'w'; fork { $stdout.puts 'Hello' }

&nbsp;&nbsp;$ cat foobar.txt  
&nbsp;&nbsp;&nbsp;&nbsp;Hello  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 4301

Am I misunderstanding you?

> - all entities which remembered $stdout instance (e.g. a logger)

I'm not sure I follow you here either.  
A reference to $stdout would persist.

&nbsp;&nbsp;foo = $stdout  
&nbsp;&nbsp;$stdout = File.new 'foobar.txt', 'w'  
&nbsp;&nbsp;# foo still references $stdout

Unless they use #dup, I don't see how the reference to $stdout wouldn't  
persist.

> Your last solution is better (although you do not necessarily need a  
> constant for that).

Yup. I just choose a constant to illustrate an example - if $stdout was  
changing often, I'd use something more appropriate.

> Btw, apparently you don't even need the mode -  
> that is inherited from the original. So you can do:
> 
> $ ruby19 -e '$stderr.reopen("log"); $stderr.puts("foo")' ; cat log  
> foo  
> $ ruby19 -e '$stdin.reopen("log"); p gets'  
> "foo\n"  
> $

Cool, good to know

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

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [11 December 2009 08:09 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/8 "2009-12-11T08:09:00Z")

</div>

> Yup, you can use IO#reopen too. I don't know about being "deprecated".
> 
> $stdout.reopen will also redirect STDOUT to the location referenced in  
> your call to #reopen. How can you redirect $stdout to its original  
> location now?
> 
> The above solution ensures you can easily switch back.

Answer to my own question:

FOO\_BAR = STDOUT.dup  
$stdout.reopen '/foo/bar/baz', 'w'  
puts 'foobar'  
$stdout.reopen FOO\_BAR

This method might be more useful if you want to redirect both $stdout,  
and STDOUT.

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 December 2009 15:32 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/9 "2009-12-11T15:32:44Z")

</div>

> > The gem was not needed for the redirection functionality but for  
> > getting at information about the method.
> 
> Yup, I completely missed that! I was tired, and misread the thread  
> completely.
> 
> > > $stdout = File.new '/path/to/file', 'w'  
> > > puts 'foobar'  
> > > $stdout = STDOUT  
> > > puts 'foobar'
> > 
> > This does only work for redirections of output for the particular  
> > process. It will fail in these cases
> > 
> > - sub processes started
> 
> Hmm really? I thought a fork() would copy the memory from the parent  
> process into the subprocess.  
> Example:
> 
> $stdout = File.open 'foobar.txt', 'w'; fork { $stdout.puts 'Hello' }
> 
> $ cat foobar.txt  
> Hello  
> =\> 4301
> 
> Am I misunderstanding you?

I probably wasn't specific enough: sub processes which use exec to  
overlay the image will not inherit redirection if you simply assign to  
$stdout:

16:24:59 ~$ ruby19 -e '$stdout = File.open("log","w"); puts 123; system("date")'  
Fri Dec 11 16:25:05 WEST 2009  
16:25:05 ~$ cat -n log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 123  
16:25:08 ~$ ruby19 -e '$stdout.reopen("log"); puts 456; system("date")'  
16:25:14 ~$ cat -n log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 456  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 Fri Dec 11 16:25:13 WEST 2009  
16:25:15 ~$

> > - all entities which remembered $stdout instance (e.g. a logger)
> 
> I'm not sure I follow you here either.  
> A reference to $stdout would persist.
> 
> foo = $stdout  
> $stdout = File.new 'foobar.txt', 'w'  
> # foo still references $stdout
> 
> Unless they use #dup, I don't see how the reference to $stdout wouldn't  
> persist.

And exactly that is a problem: redirection does not take place for  
whoever owns foo =\> it failed.

Cheers

robert

> **···**
>
> 2009/12/11 Robert Gleeson \<rob@flowof.info\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [11 December 2009 16:08 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/10 "2009-12-11T16:08:16Z")

</div>

> I probably wasn't specific enough: sub processes which use exec to  
> overlay the image will not inherit redirection if you simply assign to  
> $stdout:
> 
> 16:24:59 ~$ ruby19 -e '$stdout = File.open("log","w"); puts 123;  
> system("date")'  
> Fri Dec 11 16:25:05 WEST 2009  
> 16:25:05 ~$ cat -n log  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 123  
> 16:25:08 ~$ ruby19 -e '$stdout.reopen("log"); puts 456; system("date")'  
> 16:25:14 ~$ cat -n log  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 456  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 Fri Dec 11 16:25:13 WEST 2009  
> 16:25:15 ~$

Ah! Okay. It makes sense now 🙂 Cool, I didn't know that.

> > persist.
> 
> And exactly that is a problem: redirection does not take place for  
> whoever owns foo =\> it failed.

"whoever owns foo =\> it failed?" ..? I don't see any difference between  
assigning $stdout and using $stdout.reopen in this case.

foo = $stdout = File.open 'foo', 'w'  
foo = $stdout.reopen 'foo', 'w'

They're both references to the same object. Can you elaborate or provide  
an example? Thanks.

- Rob

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 December 2009 16:14 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/11 "2009-12-11T16:14:32Z")

</div>

> > > persist.
> > 
> > And exactly that is a problem: redirection does not take place for  
> > whoever owns foo =\> it failed.
> 
> "whoever owns foo =\> it failed?" ..? I don't see any difference between  
> assigning $stdout and using $stdout.reopen in this case.
> 
> foo = $stdout = File.open 'foo', 'w'  
> foo = $stdout.reopen 'foo', 'w'

That is not a proper model of the situation.

> They're both references to the same object. Can you elaborate or provide  
> an example? Thanks.

Now I'm tired and leave that as exercise for the reader.

Cheers

robert

> **···**
>
> 2009/12/11 Robert Gleeson \<rob@flowof.info\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [11 December 2009 16:30 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/12 "2009-12-11T16:30:45Z")

</div>

Can anyone else provide an example?

> This does only work for redirections of output for the particular  
> process. It will fail in these cases  
> - sub processes started # thats cleared up.  
> - all entities which remembered $stdout instance (e.g. a logger) # huh?

I don't see how reassignment or #reopen would make a difference. They  
reference the same object after all - for this particular case, their  
behavior is the same.

So what gives?

- Rob

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 December 2009 19:35 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/13 "2009-12-11T19:35:19Z")

</div>

I suggest you look again at my earlier posting (the one with the "date" example) and / or look at the documentation. There \*is\* a difference between reassigning and using #reopen - and it is significant.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 12/11/2009 05:30 PM, Robert Gleeson wrote:
> 
> > Can anyone else provide an example?
> > 
> > > This does only work for redirections of output for the particular  
> > > process. It will fail in these cases  
> > > - sub processes started # thats cleared up.  
> > > - all entities which remembered $stdout instance (e.g. a logger) # huh?
> > 
> > I don't see how reassignment or #reopen would make a difference. They reference the same object after all - for this particular case, their behavior is the same.
> > 
> > So what gives?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [14 December 2009 09:58 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/14 "2009-12-14T09:58:01Z")

</div>

Robert Gleeson wrote:

> I don't see how reassignment or #reopen would make a difference. They  
> reference the same object after all - for this particular case, their  
> behavior is the same.
> 
> So what gives?

When you assign to $stdout, you are only changing the Ruby global  
variable called '$stdout'

As it happens, Kernel#puts is just a shorthand for $stdout.puts, so it  
will do what you expect. But the \*real\* stdout of the process (that is,  
file descriptor 1 in the process' FD table) is untouched. You can still  
get at it using the STDOUT constant, or IO.new(1).

When you do $stdout.reopen(f), you are actually re-opening FD 1 with a  
different file - underneath, ruby is doing a dup2() call to copy  
f.fileno to FD 1. This means that the process' actual stdout has  
changed. This is what gets inherited by children you fork off.

So:

$ ruby -e '$stdout = File.open("/dev/null","w"); system("echo hello")'  
hello  
$ ruby -e '$stdout.reopen(File.open("/dev/null","w")); system("echo  
hello")'  
$

As for deprecation: I have a suspicion it's $defout not $stdout which is  
deprecated.

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

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [12 December 2009 04:29 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/15 "2009-12-12T04:29:57Z")

</div>

Robert Klemme wrote:

> > So what gives?
> 
> I suggest you look again at my earlier posting (the one with the "date"  
> example) and / or look at the documentation. There \*is\* a difference  
> between reassigning and using #reopen - and it is significant.
> 
> Cheers
> 
> &nbsp;&nbsp;robert

Yup, I've acknowledged the difference myself in all my previous posts.  
I just couldn't understand your second argument, but I think I know what  
you mean from playing around in IRB.

Reassignment will create a new object.. so

&nbsp;&nbsp;# $stdout references a new object - foo references old.  
&nbsp;&nbsp;foo = $stdout  
&nbsp;&nbsp;$stdout = File.open 'foo', 'w'

&nbsp;&nbsp;# $stdout.reopen will keep the same object identity..  
&nbsp;&nbsp;# So all previous references will persist.  
&nbsp;&nbsp;foo = $stdout  
&nbsp;&nbsp;$stdout.reopen 'foo', 'w'

Understood now. I can sleep.

- Rob

> **···**
>
> > On 12/11/2009 05:30 PM, Robert Gleeson wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [14 December 2009 14:55 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/16 "2009-12-14T14:55:33Z")

</div>

Howdy,

Thanks for clarification. If you read the entire thread, you'd see:

> > I don't see how reassignment or #reopen would make a difference. They  
> > reference the same object after all - for this particular case, their  
> > behavior is the same.
> > 
> > So what gives?

Refers to wondering how reassignment of $stdout & #reopen would make any  
difference to previous references to $stdout. It's cleared up now, as I  
understand #reopen won't change object identity but reassignment  
obviously will.

Thanks for the rest of your post.

- Rob

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

---

<div class="post-metadata">

**Author:** ![Omar\_Campos](https://avatars.discourse-cdn.com/v4/letter/o/71c47a/32.png) [@Omar\_Campos](https://rubytalk.org/u/Omar_Campos)\
**Post date:** [14 December 2009 03:12 UTC](https://rubytalk.org/t/redirecting-standard-output/56574/17 "2009-12-14T03:12:50Z")

</div>

I don't know many of the technical aspects like subprocesses, but what I  
did was this:

At the beginning of my program:  
$stderr2 = $stderr.clone  
$stderr.reopen('tmp2.dat')

At the end:  
$stderr.reopen($stderr2)  
exit

Worked like a charm!

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