Forum: Ruby A convoluted question about stdout

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.
Sonny C. (Guest)
on 2007-04-06 10:29
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.
Bruce W. (Guest)
on 2007-04-06 11:00
(Received via mailing list)
On 4/6/07, Sonny C. <removed_email_address@domain.invalid> 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?
Brian C. (Guest)
on 2007-04-06 11:22
(Received via mailing list)
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.
Sean O. (Guest)
on 2007-04-06 18:28
(Received via mailing list)
On 4/6/07, Sonny C. <removed_email_address@domain.invalid> 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
Sonny C. (Guest)
on 2007-04-06 21:06
Interesting, that's good info.  Thanks Brian.

Sonny.
Sonny C. (Guest)
on 2007-04-06 21:09
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. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.