可能重复:
Running a command from Ruby displaying and capturing the output如何执行程序并获取控制台的输出?
我有我的本地服务器上执行的程序和得到的,在一个变量,控制台的输出
可能重复:
Running a command from Ruby displaying and capturing the output如何执行程序并获取控制台的输出?
我有我的本地服务器上执行的程序和得到的,在一个变量,控制台的输出
output = `echo "hello"`
puts output # => hello
$ 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
您可以重定向输出IO重新打开。
$stdout.reopen("stdout.txt", "w")
$stderr.reopen("stderr.txt", "w")
puts 'stdout redirect'
warn 'stderr redirect'
你尝试过什么?你的代码在哪里?你收到了哪些错误信息? –
你想捕捉什么输出?其他方案的产出? – waitingkuo