[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to capture eval's stdout in 1.8?

Stephen Waits

3/7/2006 12:58:00 PM

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


3 Answers

Logan Capaldo

3/7/2006 6:28:00 PM

0


On Mar 7, 2006, at 7:58 AM, Stephen Waits 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 Waits

3/7/2006 6:29:00 PM

0

Stephen Waits wrote:
> 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
>

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 Waits

3/7/2006 7:14:00 PM

0

Logan Capaldo wrote:
>
> On Mar 7, 2006, at 7:58 AM, Stephen Waits 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