Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to use each method of launching a subprocess in Ruby
    text
    copied!<h2>1. <code>``</code> The Backtick</h2> <ul> <li><em>defined in <a href="http://www.ruby-doc.org/core/classes/Kernel.src/M001408.html" rel="noreferrer">Kernel</a></em></li> </ul> <h3>1. a) <code>%x{}</code> Percent X &lt; alternate syntax for The Backtick</h3> <ul> <li><em>defined in parse.y, see <a href="https://stackoverflow.com/questions/4287503/where-in-the-ruby-language-is-q-w-etc-defined">discussion</a></em></li> </ul> <h2>2. <code>system()</code></h2> <ul> <li><em><a href="http://www.ruby-doc.org/core/classes/Kernel.src/M001441.html" rel="noreferrer">Kernel#system</a></em></li> </ul> <h2>3. <code>fork()</code></h2> <ul> <li><em><a href="http://www.ruby-doc.org/core/classes/Kernel.src/M001439.html" rel="noreferrer">Kernel#fork, Process#fork</a></em></li> </ul> <h2>4. <code>open()</code></h2> <ul> <li><strong>open a pipe</strong></li> <li><em><a href="http://www.ruby-doc.org/core/classes/Kernel.src/M001399.html" rel="noreferrer">Kernel#open</a></em></li> </ul> <h3>4.a. <code>IO.popen()</code> &lt; behaves the same as <code>open()</code></h3> <ul> <li>open a pipe</li> <li><em><a href="http://www.ruby-doc.org/core/classes/IO.src/M000880.html" rel="noreferrer">IO#popen</a></em></li> </ul> <h3>4.b. <code>open("|-")</code></h3> <ul> <li><strong>fork to a pipe</strong></li> </ul> <h3>4.c. <code>IO.popen("-")</code> &lt; behaves the same as <code>open("|-")</code></h3> <ul> <li>fork to a pipe</li> <li>see <a href="https://stackoverflow.com/questions/5611752/ruby-io-popen-with-what-happens-under-the-hood">discussion</a></li> </ul> <h2>5. <code>Open3.popen3()</code></h2> <ul> <li><code>require 'open3'</code></li> <li>stdlib <a href="http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/classes/Open3.html" rel="noreferrer">Open3</a></li> </ul> <h2>6. <code>PTY.spawn()</code></h2> <ul> <li><code>require 'pty'</code></li> <li>stdlib <a href="http://www.ruby-doc.org/stdlib/libdoc/pty/rdoc/classes/PTY.html" rel="noreferrer">PTY</a></li> </ul> <h2>7. <code>Shell.transact()</code></h2> <ul> <li><code>require 'shell'</code></li> <li>stdlib <a href="http://www.ruby-doc.org/stdlib/libdoc/shell/rdoc/classes/Shell.html" rel="noreferrer">Shell</a></li> </ul> <h2>When should one forsake the trusty back-tick for one of the more complex methods?</h2> <p><strong>Edit 1.</strong> Big thanks to <a href="https://stackoverflow.com/users/20487/avdi">Avdi Grimm</a> for his posts describing example usage of each method: <a href="http://devver.wordpress.com/2009/06/30/a-dozen-or-so-ways-to-start-sub-processes-in-ruby-part-1/" rel="noreferrer">#1</a> (&amp; <a href="http://gist.github.com/137705" rel="noreferrer">gist</a>); <a href="http://devver.wordpress.com/2009/07/13/a-dozen-or-so-ways-to-start-sub-processes-in-ruby-part-2/" rel="noreferrer">#2</a> (&amp; <a href="https://gist.github.com/146199" rel="noreferrer">gist</a>); <a href="http://devver.wordpress.com/2009/10/12/ruby-subprocesses-part_3/" rel="noreferrer">#3</a>.</p> <p>They are fantastic resources to answer <em>How</em>, but are not explicitly composed to answer <em>when each should be used</em> or <em>Why</em>, and as such IMHO are not complete answers to this question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload