Yes. There are several ways:
a. Use %x
or '`':
%x(echo hi) #=> "hi\n"
%x(echo hi >&2) #=> "" (prints 'hi' to stderr)
`echo hi` #=> "hi\n"
`echo hi >&2` #=> "" (prints 'hi' to stderr)
These methods will return the stdout, and redirect stderr to the program's.
b. Use system
:
system 'echo hi' #=> true (prints 'hi')
system 'echo hi >&2' #=> true (prints 'hi' to stderr)
system 'exit 1' #=> nil
This method returns true
if the command was successful. It redirects all output to the program's.
c. Use exec
:
fork { exec 'sleep 60' } # you see a new process in top, "sleep", but no extra ruby process.
exec 'echo hi' # prints 'hi'
# the code will never get here.
That replaces the current process with the one created by the command.
d. (ruby 1.9) use spawn
:
spawn 'sleep 1; echo one' #=> 430
spawn 'echo two' #=> 431
sleep 2
# This program will print "two\none".
This method does not wait for the process to exit and returns the PID.
e. Use IO.popen
:
io = IO.popen 'cat', 'r+'
$stdout = io
puts 'hi'
$stdout = IO.new 0
p io.read(1)
io.close
# prints '"h"'.
This method will return an IO
object that reperesents the new processes' input/output. It is also currently the only way I know of to give the program input.