tags:

views:

47

answers:

2

I have to execute a shell command from Ruby script but I have to retrieve the output so that I can use it in the script later on.

Here is my code:

output = system "heroku create" # => true

But the system command returns a boolean and not the output.

Simply said, system "heroku create" has to output to my screen (which it does) but also return the output so I can process it.

+5  A: 

You could use

output = `heroku create`

See: http://ruby-doc.org/core/classes/Kernel.html

NullUserException
is it the same as %x("heroku create") ?
never_had_a_name
@ajsie According to the manual, "The built-in syntax `%x{…}` uses this method. "
NullUserException
http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/
tokland
+1  A: 

The open3 library gives you full access to the standard io streams (input, output and error). It's part of ruby. No need to install any gem. Eg:

require 'open3'

stdin, stdout, stderr = Open3.popen3("heroku create")
puts stdout.read
Alkaline