3 ms·
> Ruby has no built-in for "call a subprocess and convert a nonzero exit status into an exception" Since Ruby 2.6 you can pass `exception: true` to `system` to
by copirate 2y ago
> Ruby has no built-in for "call a subprocess and convert a nonzero exit status into an exception"
Since Ruby 2.6 you can pass `exception: true` to `system` to make it behave like your `system!`.
https://rubyreferences.github.io/rubychanges/2.6.html#system-has-exception-argument https://rubyreferences.github.io/rubychanges/2.6.html#system...
- derefr 2y agoDidn't realize that! That's one snippet I can maybe eliminate now. (As to why I didn't know: the first thing in the RDoc for Kernel#system is still "see the docs for Kernel#spawn for options" — and then Kernel#spawn doesn't actually have that one, because it doesn't block until the process quits, and so returns you a pid, not a Process::Status. I stopped looking at the docs for Kernel#system itself a long time ago, just jumping directly to Kernel#spawn...) But come to think of it, if Kernel#system is just doing a blocking version of Kernel#spawn → Process#wait, then shouldn't Process#wait also take an exception: kwarg now? And also-also, sadly IO.popen doesn't take this kwarg. (And IO.popen is what I'm actually using most of the time. The system! function above is greatly simplified from the version of the snippet I actually use these days — which involves a DSL for hierarchical serial task execution that logs steps with nesting, and reflects command output from an isolated PTY.)