您可以使用IO.pipe
并告诉Process.spawn
使用重定向的输出,而无需外部gem。
当然,仅从Ruby 1.9.2开始(我个人建议1.9.3)
以下是Spinach BDD在内部用于捕获输出和err输出的简单实现:
# stdout, stderr pipes
rout, wout = IO.pipe
rerr, werr = IO.pipe
pid = Process.spawn(command, :out => wout, :err => werr)
_, status = Process.wait2(pid)
# close write ends so we Could read them
wout.close
werr.close
@stdout = rout.readlines.join("\n")
@stderr = rerr.readlines.join("\n")
# dispose the read ends of the pipes
rout.close
rerr.close
@last_exit_status = status.exitstatus
原始来源位于features / support / filesystem.rb中
强烈建议您阅读Ruby自己的Process.spawn文档。
希望这可以帮助。
PS:我将超时实现留给您做功课;-)