Python provides two convenient functions for calling subprocesses that might fail, subprocess.check_call
and subprocess.check_output
. Basically,
subprocess.check_call(['command', 'arg1', ...])
spawns the specified command as a subprocess, blocks, and verifies that the subprocess terminated successfully (returned zero). If not, it throws an exception. check_output
does the same thing, except it captures the subprocess's stdout and returns it as a byte-string.
This is convenient because it is a single Python expression (you don't have to set up and control the subprocess over several lines of code), and there's no risk of forgetting to check the return value.
What are the idiomatic Ruby equivalents to check_call
and check_output
? I am aware of the $?
global that gives the process's return value, but that would be awkward—the point of having exceptions is that you don't have to manually check error codes. There are numerous ways to spawn a subprocess in Ruby, but I don't see any that provide this feature.