Phlip wrote: > Rubies: > > The popen (and popen3) documentation and discussions in the 'net archives > deal with opening a program and only reading from it. > > I want to have a QA session. So here's a Ruby program (save it as "qa.rb") > that asks a question, collects an answer, and prints it: > > print 'question? ' > STDOUT.flush() > answer = gets() > puts answer > STDOUT.flush() > > Now that has excess flushes, but they don't seem to help. Here's a program > that tries to run that program, answer its question, and collect the > bounced answer. It has excess flushes too. It also cheats by knowing the > length of the question. My target program can cheat like this too: And here's the winner: require 'fcntl' require 'open3' def pump contents = '' pin, pout, perr = Open3.popen3('ruby qa.rb') pin.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) pout.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) perr.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) loop do retval = select([pout, perr, $stdin], nil, [pin, $stdout, $stderr], nil) # puts "select returned" for i in retval[0] str = i.read(1) print str return if str == nil contents += str if contents.size() == 9 then pin.write("yo\n") print "yo\n" end STDOUT.flush() end end end pump() -- Phlip http://andstuff.org/HarryPotter -- Just think: Four billion people in the world have never received Spam... --