From: Yohanes Santoso Date: 2002-06-16T09:51:45+09:00 Subject: Re: Question: how to do (repeated) bidirectional communication with another process via stdin/stdout ? ke = IO.popen("cat","r+") loginCall = "blablabla\n" # cat needs \n at the end before it echoes back the line sysInfoCall = "fofofofo\n" if (ke != nil) ke.syswrite(loginCall) # #flush() is not needed if you use syswrite, as #flush only flushes # ruby's internal buffering and ruby does not perform any buffering # with syswrite and sysread #ke.flush() # #each("\n") blocks until it finds \n or EOF. #ke.each("\n") { |line| puts(line) } # if you don't know the number of lines to be read, then you have to use # IO#select so your program does not block unnecessarily. # alternatively if you now the number of lines to be read, just invoke # #readline that many times. puts ke.readline ke.syswrite(sysInfoCall) #ke.flush() # ditto, syswrite does not require flush ke.close_write # the #each here works because the condition \n or EOF is true. ke.each("\n") { |line| puts(line) } ke.close end