1 min read

Open3

Ruby's has lots of ways of forking a process, the obsure one is the best
Open3
Photo by Finn Hackshaw / Unsplash

Ruby's has lots of ways of forking a process, the obsure one is the best.

The usual go-to for running processes is one of these:

stdout = `ls` # backticks, returns stdout
success = system('ls') # returns true if exit code == 0

But what if I want to see stderr? The strangely named Open3 to the rescue. It has high and lower level APIs for doing just that. Here, I'm looking at the higher level API.

require 'open3'
std_out, std_err, status = Open3.capture3('ls')

Because you really don't want to fork a system process with arbitrary input, it allows the separation of command and parameters too:

std_out, std_err, status = Open3.capture3('ls', my_directory)

And status is a nice Process::Status object that I can ask about exit codes and such like.