2012-06-15 118 views

回答

2
output = `echo "hello"` 
puts output # => hello 
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 
0

您可以重定向输出IO重新打开。

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

puts 'stdout redirect' 
warn 'stderr redirect' 
相关问题