Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a replacement for Ruby's IO.popen() and system()

Tags:

ruby

IO.popen() and system() in Ruby is sorely lacking several useful features, such as:

  • obtaining the return value of the function
  • capturing both stdout and stderr (seperately and merged)
  • running without spawning an extra cmd.exe or /bin/sh process

Python has a module "subprocess" which I was thinking about using as inspiration for a similar module in Ruby. Now to the questions:

  • How are Ruby-programmers working around the issues above, for example obtaining the return value when doing a popen() call?
  • Is this something which has already been implemented?
like image 938
JesperE Avatar asked Sep 30 '08 19:09

JesperE


2 Answers

Take a look at the standard Ruby library open3. This will give you access to stdin, stdout and stderr.

There is also an external project called open4, which allows you to get the exit status without using a magic variable name.

like image 93
Aaron Hinni Avatar answered Sep 20 '22 13:09

Aaron Hinni


  • system() exit status can be captured with $?.exitstatus
  • stderr can be captured with something like system 'command 2>&1'
like image 39
Eric Freese Avatar answered Sep 18 '22 13:09

Eric Freese