从Ruby运行交互式程序 [英] Running an interactive program from Ruby
本文介绍了从Ruby运行交互式程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从红宝石(不使用外部gem)运行gnuplot并解析其文本输出.我尝试了IO.popen
,PTY.spawn
和Open3.popen3
,但是每当我尝试获取输出时,它只会挂起"-我想等待更多的输出.我觉得使用Thread.new
以某种方式完成了该操作,但是我找不到实现它的正确方法.
I am trying to run gnuplot from ruby (not using an external gem) and parsing its textual output also. I tried IO.popen
, PTY.spawn
and Open3.popen3
but whenever I try to get the output it just "hangs" -I guess waiting for more output to come. I feel like its somehow done using Thread.new
but I couldn't find the correct way to implement it.
有人知道怎么做吗?
推荐答案
我想这就是您想要的:
require 'pty'
require 'expect'
PTY.spawn('gnuplot') do |input, output, pid|
str = input.expect(/gnuplot>/)
puts str
output.puts "mlqksdf"
str = input.expect(/gnuplot>/)
puts str
output.puts "exit"
end
这篇关于从Ruby运行交互式程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文