2

I want to run a program from within my Ruby script, but I want to capture the program's STDOUT and STDERR separately, without intermingling them, thus doing 2>&1 on the command shell won't do it for me.

I'd really prefer not to have to direct these to a tempfile and read the tempfile back in to my script. Is there a way I can directly get both of these in my Ruby script?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
tamouse
  • 2,169
  • 1
  • 19
  • 26
  • Related questions: http://stackoverflow.com/questions/3018595/how-do-i-redirect-stderr-and-stdout-to-file-for-a-ruby-script http://stackoverflow.com/questions/4459330/how-do-i-temporarily-redirect-stderr-in-ruby – rhetonik Nov 30 '12 at 05:36
  • @rhetonik: Not really, tamouse wants to capture stdout and stderr from a subprocess. – mu is too short Nov 30 '12 at 05:38

1 Answers1

7

You should use the Open3 class. It provides methods to execute shell commands that can return stdin, stdout, and stderr as separate IO objects.

http://ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-popen3

Jake Dempsey
  • 6,264
  • 1
  • 30
  • 25
  • In particular, look at [`capture3`](http://ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-capture3). – the Tin Man Nov 30 '12 at 06:22