Reputation: 2218
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?
Upvotes: 2
Views: 1658
Reputation: 6312
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
Upvotes: 7