-4

Possible Duplicate:
Running a command from Ruby displaying and capturing the output

I have to execute a program on my local server and get, in a variable, the console's output

Community
  • 1
  • 1
sparkle
  • 6,940
  • 19
  • 64
  • 107

3 Answers3

2
output = `echo "hello"`
puts output # => hello
Samy Dindane
  • 16,786
  • 3
  • 39
  • 49
1
$ irb
1.9.3p125 :001 > cal = %x[/usr/bin/cal]
1.9.3p125 :002 > puts cal
     June 2012        
Su Mo Tu We Th Fr Sa  
                1  2  
 3  4  5  6  7  8  9  
10 11 12 13 14 15 16  
17 18 19 20 21 22 23  
24 25 26 27 28 29 30  
Lars Haugseth
  • 14,302
  • 2
  • 43
  • 47
0

You can redirect output with IO reopen.

$stdout.reopen("stdout.txt", "w")
$stderr.reopen("stderr.txt", "w")

puts 'stdout redirect'
warn 'stderr redirect'
YYZ
  • 699
  • 4
  • 8
  • 18