From: matz@... (Yukihiro Matsumoto) Date: 2000-11-18T14:11:26+09:00 Subject: [ruby-talk:6436] Re: Two-Way Pipes with popen Hi. In message "[ruby-talk:6431] Two-Way Pipes with popen" on 00/11/18, jweirich@one.net writes: |I want to start a process and feed it information through its standard |input and read the result from its standard output. The "popen" |command seems to do one or the other, but not both at once. It there |a way to do this? (Currently I'm using a work-around using a |temporary file). Try open3 library. a = Open3.popen3("nroff -man") Thread.start do while line = gets a[0].print line end a[0].close end while line = a[1].gets print ":", line end |In a similar vein, is there an "expect"-like addon for Ruby? I'm using a stuff like this: class Expecter def initialize(timeout=nil, dynamic=nil) @timeout=timeout @action= {} @dynamic=dynamic end def on(pattern, action=lambda) @action[pattern] = action end def wait(on) keys = @action.keys while TRUE fds = select([on], nil, nil) unless fds if @action['TIMEOUT'] @action['TIMEOUT'].call end return end break if fds.size == 0 or fds[0].size == 0 line = on.gets break unless line print line for key in keys if line =~ key @action[key].call(line) end end keys = @action.keys if @dynamic end if @action['EOF'] @action['EOF'].call end end def expect(on) fds = select([on], nil, nil, @timeout) unless fds if @action['TIMEOUT'] @action['TIMEOUT'].call end return end return if fds.size == 0 or fds[0].size == 0 line = on.gets unless line if @action['EOF'] @action['EOF'].call end return end print line for key in @action.keys if line =~ key @action[key].call(line) end end end end expect = Expecter.new expect.on(/remote IP address/) do print "connection established\n" exit end expect.on(/[^n]REDIAL ERROR/) do exit end f = open("|tail -n 0 -f /var/log/messages") expect.wait(f)