-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
  • 7,530
  • 22
  • 69
  • 131

3 Answers3

2
output = `echo "hello"`
puts output # => hello
Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
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,721
  • 2
  • 45
  • 49
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
  • 749
  • 4
  • 8
  • 18