Forum: Ruby How to capture eval's stdout in 1.8?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Stephen W. (Guest)
on 2006-03-07 15:00
(Received via mailing list)
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
Logan C. (Guest)
on 2006-03-07 20:30
(Received via mailing list)
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. (Guest)
on 2006-03-07 20:30
(Received via mailing list)
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
Stephen W. (Guest)
on 2006-03-07 21:15
(Received via mailing list)
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
This topic is locked and can not be replied to.