Shell pipeline in Ruby?


On 12 May 2011 18:05, Michal S. removed_email_address@domain.invalid wrote:

cmdA directly, on its stdin, and similarily for cmdC and cmdB.

Here I am not interested in feeding cmdA some particular input (eg. it
can read /dev/null for all I care) but in general gluing arbitrary fd
to its stdin might be desirable in other cases.

Thanks for all the replies.

To sum up there are multiple methods to achieve this.

  • fork - works on ruby 1.8 but not on Windows
  • Open3 - very nice interface with pipeline_start and such which
    does much of the setup behind the scenes but from what I read on the
    Net does not work on Windows, again
  • Open3-win32 and Open4 exist to address this issue but they may not
    have the same interface.
  • Process.spawn - works only in ruby 1.9 but should be portable to
    Windows should you need it. Basically equivalent to Open3 except you
    see all the gory details. Not sure why Open3 does not use this on
    Windows. Maybe Open3 was in 1.8 already when spawn did not allow for
    redirects or something.
  • Shell - somewhat not well known and not well documented Ruby
    library. You will reimplement this if you use one of the above :wink:

For reading and writing numerous pipes you can use either a Ruby
thread per pipe or a select loop, either works.