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:
require 'fcntl' pipe = IO.popen( "ruby qa.rb", "r+" ) # pipe.close_write() pipe.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) def absorb pipe contents = '' loop do begin nu = pipe.readchar() rescue nu = nil end contents += nu.chr() if nu != nil break if contents.size() > 9 end puts contents end absorb pipe pipe.write('yo\n') pipe.flush() absorb pipe
Please observe I’m not asking how to popen into the ruby executable itself.
My ultimate target is a binary executable, so telling me how to change the
options of the inner ruby won’t help.
– Got in trouble at StarBucks. I tried to order
"A double latte mocha and a body piercing." –