# How to capture eval's stdout in 1.8?

**URL:** <https://www.ruby-forum.com/t/how-to-capture-evals-stdout-in-1-8/54513>\
**Category:** Ruby\
**Created:** [March 7, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-capture-evals-stdout-in-1-8/54513 "2006-03-07T14:00:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [March 7, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-capture-evals-stdout-in-1-8/54513/1 "2006-03-07T14:00:09Z")

</div>

My searches have come up empty, and most of what I found refers to 1.6  
(assigning $defout to a StringIO instance).

However, under 1.8 this doesn’t work because:

a) assignment to $stdout (which deprecates $defout) is deprecated  
b) $stdout.reopen( StringIO ) doesn’t work as reopen expects a true IO  
instance

And, for that matter, I couldn’t even get it to redirect to a File.

Seems like it should be easy… anyone?

Thanks,  
Steve

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [March 7, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-capture-evals-stdout-in-1-8/54513/2 "2006-03-07T19:30:16Z")

</div>

On Mar 7, 2006, at 7:58 AM, Stephen W. wrote:

> a) assignment to $stdout (which deprecates $defout) is deprecated

Are you sure? -w gives me no Object#type is deprecated use  
Object#class style warnings

% cat moving\_stdout.rb  
require ‘stringio’  
old\_stdout = $stdout  
$stdout = StringIO.new  
puts “Hello”  
old\_stdout.puts “The real console”  
old\_stdout.puts “Caputured stdout: #{$stdout.string}”  
$stdout = old\_stdout  
puts “Back to normal”

% ruby -w moving\_stdout.rb  
The real console  
Caputured stdout: Hello  
Back to normal

% ruby -v  
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.4.0]

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [March 7, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-capture-evals-stdout-in-1-8/54513/3 "2006-03-07T19:30:17Z")

</div>

Stephen W. wrote:

> Seems like it should be easy… anyone?
> 
> Thanks,  
> Steve

Hmm… I still haven’t found the right solution. I’m starting to worry  
that this might not be possible in 1.8!

–Steve

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [March 7, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/how-to-capture-evals-stdout-in-1-8/54513/4 "2006-03-07T20:15:52Z")

</div>

Logan C. wrote:

> On Mar 7, 2006, at 7:58 AM, Stephen W. wrote:
> 
> > a) assignment to $stdout (which deprecates $defout) is deprecated
> 
> Are you sure? -w gives me no Object#type is deprecated use Object#class  
> style warnings

Shame on me. I followed the docs too closely, which all say assignment  
is deprecated, use .reopen.

Anyway, it works!

Thanks Logan.

–Steve
