A convoluted question about stdout

Hey Guys,

I know I can do a $stdout.reopen and redirect it to a file. Is there a
more direct way to get at the contents of stdout… say, redirect stdout
to variable?

Sonny.

On 4/6/07, Sonny C. [email protected] wrote:

Hey Guys,

I know I can do a $stdout.reopen and redirect it to a file. Is there a
more direct way to get at the contents of stdout… say, redirect stdout
to variable?

#!/usr/bin/env ruby

old_std_out = $stdout
$stdout = File.open(“/tmp/file”, “w+”)

puts “where does the output go?”
bruce@carlpowerbook:~/tmp$ rm -f /tmp/file
bruce@carlpowerbook:~/tmp$ ./r.rb
bruce@carlpowerbook:~/tmp$ cat /tmp/file
where does the output go?
bruce@carlpowerbook:~/tmp$

or, with build in clean up…

bruce@carlpowerbook:~/tmp$ rm -f /tmp/file
bruce@carlpowerbook:~/tmp$ cat r.rb
#!/usr/bin/env ruby

def with_stdout(file_name)
old_stdout = $stdout
$stdout = File.open(file_name, “w+”)
yield
$stdout = old_stdout
end

with_stdout “/tmp/file” do
puts “where does the output go?”
end

puts “and where is stdout now?”
bruce@carlpowerbook:~/tmp$ ./r.rb
and where is stdout now?
bruce@carlpowerbook:~/tmp$ cat /tmp/file
where does the output go?

On Fri, Apr 06, 2007 at 03:29:12PM +0900, Sonny C. wrote:

I know I can do a $stdout.reopen and redirect it to a file.

Note that STDOUT is tied to the underlying operating system’s concept of
“standard output” - for Unix this is the open file on file descriptor 1.
So
if you reopen $stdout (or STDOUT) you’ll change what fd 1 points at.
Then,
anything else which writes directly to fd 1 - e.g. C extensions, or
external
programs run using system() or exec() - will also write to this file.

Now, you can change the global variable $stdout to point to a different
IO
object. In that case, anything in Ruby which does “$stdout.puts” will
write
to that new object. But anything which writes directly to fd 1 will
still be
writing to whatever the OS has attached to fd 1.

Regards,

Brian.

On 4/6/07, Sonny C. [email protected] wrote:

Maybe something like this (a variation on Bruce’s post):

def capture_stdout(&block)
raise ArgumentError, “No block given” if !block_given?
old_stdout = $stdout
$stdout = sio = StringIO.new
yield
$stdout = old_stdout
sio.rewind
sio.read
end

txt = capture_stdout do
puts “dlroW olleH”
end
puts txt.reverse

Regards,
Sean

Thanks Sean. This is exactly what I was looking for… for some reason
I overlooked the StringIO class when I was perusing the Standard Library
listing.

Sonny.

Sean O’halpin wrote:

On 4/6/07, Sonny C. [email protected] wrote:

Maybe something like this (a variation on Bruce’s post):

def capture_stdout(&block)
raise ArgumentError, “No block given” if !block_given?
old_stdout = $stdout
$stdout = sio = StringIO.new
yield
$stdout = old_stdout
sio.rewind
sio.read
end

txt = capture_stdout do
puts “dlroW olleH”
end
puts txt.reverse

Regards,
Sean

Interesting, that’s good info. Thanks Brian.

Sonny.