Ruby出现时可以从shell命令输出输出吗?

Cir*_*yon 12 ruby shell scripting command-prompt

我的Ruby脚本正在运行shell命令并解析它的输出.但是,似乎首先执行命令并将输出保存在数组中.我希望能够像打印时那样实时访问输​​出线.我玩过线程,但还没有开始工作.有什么建议?

Mar*_*rth 21

你在寻找管道.这是一个例子:

# This example runs the netstat command via a pipe
# and processes the data in Ruby as it come back

pipe = IO.popen("netstat 3")
while (line = pipe.gets)
  print line
  print "and"
end
Run Code Online (Sandbox Code Playgroud)