# Capturing standard out?

**URL:** <https://rubytalk.org/t/capturing-standard-out/36275>\
**Category:** ruby-talk\
**Created:** [19 March 2007 22:18 UTC](https://rubytalk.org/t/capturing-standard-out/36275 "2007-03-19T22:18:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [19 March 2007 22:18 UTC](https://rubytalk.org/t/capturing-standard-out/36275/1 "2007-03-19T22:18:23Z")

</div>

Is there an easy way to capture standard out in Ruby, so that "puts"  
throws its output into a buffer instead of just popping up on the  
screen?

> **···**
>
> --  
> Giles Bowkett  
> [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)
> 
> > **[Giles Bowkett](http://gilesbowkett.blogspot.com)**
> >
> > half-disavowed old blog. new blog at gilesbowkett.com
> 
>   
> 
> > **[Wixel](https://giles.tumblr.com/#_=_)**
> >
> > I am free, That is why i'm Lost.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [19 March 2007 22:43 UTC](https://rubytalk.org/t/capturing-standard-out/36275/2 "2007-03-19T22:43:02Z")

</div>

I have a scriopt producing output for bash, for running the test spec  
I plugin output into an array, more or less like this:

class Output  
&nbsp;&nbsp;&nbsp;@data =  
&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :data  
&nbsp;&nbsp;end  
end  
if $TESTING then  
&nbsp;&nbsp;&nbsp;def Kernl.puts \*args, &blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Output.data \<\< args.join("")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Output.data \<\< blk.call if blk  
&nbsp;&nbsp;&nbsp;end  
end

I have also Output.data.clear in #setup of the testsuite.

HTH  
Robert

> **···**
>
> On 3/19/07, Giles Bowkett \<gilesb@gmail.com\> wrote:
> 
> > Is there an easy way to capture standard out in Ruby, so that "puts"  
> > throws its output into a buffer instead of just popping up on the  
> > screen?
> > 
> > --  
> > Giles Bowkett  
> > [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> > [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> > [http://giles.tumblr.com/](http://giles.tumblr.com/)
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [19 March 2007 22:52 UTC](https://rubytalk.org/t/capturing-standard-out/36275/3 "2007-03-19T22:52:34Z")

</div>

This is exactly the problem shell IO redirection was designed to solve.

$ ruby script.rb \> output

What is the use case that prevents you from using shell redirection?

Gary Wright

> **···**
>
> On Mar 19, 2007, at 6:18 PM, Giles Bowkett wrote:
> 
> > Is there an easy way to capture standard out in Ruby, so that "puts"  
> > throws its output into a buffer instead of just popping up on the  
> > screen?

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [19 March 2007 23:08 UTC](https://rubytalk.org/t/capturing-standard-out/36275/4 "2007-03-19T23:08:33Z")

</div>

You can set the $stdout global variable to point to whatever object you  
like, as long as it adheres to a simple interface. Kernel#puts simply  
redirects to $stdout.

Cheers,  
Daniel

> **···**
>
> On Tue, 2007-03-20 at 07:18 +0900, Giles Bowkett wrote:
> 
> > Is there an easy way to capture standard out in Ruby, so that "puts"  
> > throws its output into a buffer instead of just popping up on the  
> > screen?

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [26 March 2007 15:53 UTC](https://rubytalk.org/t/capturing-standard-out/36275/5 "2007-03-26T15:53:17Z")

</div>

I would do it like this:

#!/usr/bin/env ruby -w

require "stringio"

def capture\_stdout(buffer = StringIO.new)  
&nbsp;&nbsp;&nbsp;saved\_stdout = $stdout  
&nbsp;&nbsp;&nbsp;$stdout = buffer

&nbsp;&nbsp;&nbsp;yield

&nbsp;&nbsp;&nbsp;$stdout = saved\_stdout

&nbsp;&nbsp;&nbsp;buffer.string rescue buffer  
end

if \_\_FILE\_\_ == $PROGRAM\_NAME  
&nbsp;&nbsp;&nbsp;puts "This will be printed."  
&nbsp;&nbsp;&nbsp;output = capture\_stdout { puts "Meanwhile, this was captured." }  
&nbsp;&nbsp;&nbsp;puts "This also will be printed."  
&nbsp;&nbsp;&nbsp;p output  
end

\_\_END\_\_

James Edward Gray II

> **···**
>
> On Mar 19, 2007, at 5:18 PM, Giles Bowkett wrote:
> 
> > Is there an easy way to capture standard out in Ruby, so that "puts"  
> > throws its output into a buffer instead of just popping up on the  
> > screen?

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [19 March 2007 23:52 UTC](https://rubytalk.org/t/capturing-standard-out/36275/6 "2007-03-19T23:52:06Z")

</div>

> \> Is there an easy way to capture standard out in Ruby, so that "puts"  
> \> throws its output into a buffer instead of just popping up on the  
> \> screen?
> 
> You can set the $stdout global variable to point to whatever object you  
> like, as long as it adheres to a simple interface. Kernel#puts simply  
> redirects to $stdout.

That is a good idea too, especially with this idiom

def xxx(..., out = $stdout)

Robert

> **···**
>
> On 3/19/07, Daniel Schierbeck \<daniel.schierbeck@gmail.com\> wrote:
> 
> > On Tue, 2007-03-20 at 07:18 +0900, Giles Bowkett wrote:
> > 
> > Cheers,  
> > Daniel
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [20 March 2007 00:49 UTC](https://rubytalk.org/t/capturing-standard-out/36275/7 "2007-03-20T00:49:11Z")

</div>

Hey Daniel, I found an example of the $stdout technique from Matz:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/15519](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/15519)

That's what I ended up using, although I used $stdout and instead of  
$defout, and the documentation I found said assigning to $stdout is  
deprecated in favor of $stdout.reopen, which I couldn't seem to get to  
work for me.

> **···**
>
> On 3/19/07, Daniel Schierbeck \<daniel.schierbeck@gmail.com\> wrote:
> 
> > On Tue, 2007-03-20 at 07:18 +0900, Giles Bowkett wrote:  
> > \> Is there an easy way to capture standard out in Ruby, so that "puts"  
> > \> throws its output into a buffer instead of just popping up on the  
> > \> screen?
> > 
> > You can set the $stdout global variable to point to whatever object you  
> > like, as long as it adheres to a simple interface. Kernel#puts simply  
> > redirects to $stdout.
> > 
> > Cheers,  
> > Daniel
> 
> --  
> Giles Bowkett  
> [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)
> 
> > **[Giles Bowkett](http://gilesbowkett.blogspot.com)**
> >
> > half-disavowed old blog. new blog at gilesbowkett.com
> 
> [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [20 March 2007 00:50 UTC](https://rubytalk.org/t/capturing-standard-out/36275/8 "2007-03-20T00:50:59Z")

</div>

I have no idea, I'm solving this for somebody I work with, but I'm  
pretty confident they're familiar with shell redirection already.  
Thanks tho.

> **···**
>
> On 3/19/07, Gary Wright \<gwtmp01@mac.com\> wrote:
> 
> > What is the use case that prevents you from using shell redirection?
> 
> --  
> Giles Bowkett  
> [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)
> 
> > **[Giles Bowkett](http://gilesbowkett.blogspot.com)**
> >
> > half-disavowed old blog. new blog at gilesbowkett.com
> 
> [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![Erik\_Veenstra1](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@Erik\_Veenstra1](https://rubytalk.org/u/Erik_Veenstra1)\
**Post date:** [20 March 2007 18:05 UTC](https://rubytalk.org/t/capturing-standard-out/36275/9 "2007-03-20T18:05:38Z")

</div>

Here's a code snippet from my log library.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

> **···**
>
> ----------------------------------------------------------------
> 
> ["$stdout", "$stderr"].each do |std|  
> &nbsp;&nbsp;&nbsp;io = eval(std)  
> &nbsp;&nbsp;&nbsp;old\_write = io.method(:write)
> 
> &nbsp;&nbsp;&nbsp;class \<\< io  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
> &nbsp;&nbsp;&nbsp;end.module\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:write) do |text|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless text =~ /^[\r\n]+$/ # Because puts calls twice.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.open("logfile.log", "a") do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.puts [std[1..-1].upcase, caller[2], text].join(" ")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_write.call(text)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> $stdout.puts "text on stdout"  
> $stderr.puts "text on stderr"
> 
> ----------------------------------------------------------------
