How to capture eval's stdout in 1.8?

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

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]

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

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