tamouse
tamouse

Reputation: 2218

How do I get both STDOUT and STDERR separately from a process call in Ruby?

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

Answers (1)

Jake Dempsey
Jake Dempsey

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

Related Questions