tags:

views:

18

answers:

1
pipe = IO.popen("my_cmd 2>&0")
while ???
  line = pipe.gets
  puts line if some_condition
end

This is using Ruby 1.8.7 on Windows. my_cmd is an application that prints database records to the stdout. One line per database record, and there's no way to know how many records there will be before I run the command. Each gets call returns one record. Each gets call could take a while, too. It's going over a network to a very large database, so the gets call won't be instant.

I'm processing the input just fine. The problem is, the loop never ends.

my_cmd terminates when it reaches the end of the database. How does my Ruby script know when my_cmd is done? What should ??? be in the code I posted above?

+2  A: 

Either

  while !out.eof?

or when you

 Process.wait pipe.pid

http://en.wikibooks.org/wiki/Ruby_Programming/Running_Multiple_Processes

rogerdpack
do you mean `!pipe.eof?`?
John Dibling
This worked beautifully. I swear I tried this already and it didn't work. But now it does!
John Dibling