views:

264

answers:

2

Sorry if this is a naïve question, but let's say I have a Ruby program called processor.rb that begins with data = STDIN.read. If I invoke this program like this

cat textfile.txt | processor.rb

Does STDIN.read wait for cat to pipe the entire textfile.txt in? Or does it assign some indeterminate portion of textfile.txt to the data variable?

I'm asking this because I recently saw a strange bug in one of my programs that suggests that the latter is the case.

A: 

Probably is line buffered and reads until it encounters a newline or EOF.

GregS
`IO.read` will read until EOF if no argument is provided.
Inshallah
+1  A: 

The read method should import the entire file, as-is, and return only when the process producing the output has finished, as indicated by a flag on the pipe. It should be the case that on output from cat that if you call read a subsequent time, you will return 0 bytes.

In simple terms, a process is allowed to append to its output at any time, which is the case of things like 'tail -f', so you can't be assured that you have read all the data from STDIN without actually checking.

Your OS may implement cat or shell pipes slightly differently, though. I'm not familiar with what POSIX dictates for behavior here.

tadman