场景:
我必须从我的Ruby脚本调用外部程序,并且该程序向stdout和stderr发送了许多有用的(但含糊不清的)信息.
程序运行时,我想解析它发送到stdout和stderr的行:
我尝试了所有常用的技巧(系统,exec,popen,popen3,反引号等等),但我只能在程序执行后检索stdout/stderr ,而不是在执行期间.
有任何想法吗?
哦,我在Windows上:-(
h3r*_*ald 15
实际上,它比我想象的要简单,这看起来很完美:
STDOUT.sync = true # That's all it takes...
IO.popen(command+" 2>&1") do |pipe| # Redirection is performed using operators
pipe.sync = true
while str = pipe.gets
puts "-> "+str # This is synchronous!
end
end
Run Code Online (Sandbox Code Playgroud)
...是的,它适用于Windows!