lib/io_streams/line/reader.rb in iostreams-1.9.0 vs lib/io_streams/line/reader.rb in iostreams-1.10.0

- old
+ new

@@ -73,10 +73,12 @@ # Iterate over every line in the file/stream passing each line to supplied block in turn. # Returns [Integer] the number of lines read from the file/stream. # Note: # * The line delimiter is _not_ returned. def each + return to_enum(__method__) unless block_given? + line_count = 0 until eof? line = readline unless line.nil? yield(line) @@ -144,22 +146,23 @@ end data end - # Returns [Integer] the number of characters read into the internal buffer - # Returns 0 on EOF + # Returns whether more data is available to read + # Returns false on EOF def read_block return false if @eof block = if @read_cache_buffer begin @input_stream.read(@buffer_size, @read_cache_buffer) rescue ArgumentError # Handle arity of -1 when just 0..1 - @read_cache_buffer = nil + @read_cache_buffer = nil + @use_read_cache_buffer = false @input_stream.read(@buffer_size) end else @input_stream.read(@buffer_size) end @@ -167,9 +170,12 @@ # EOF reached? if block.nil? @eof = true return false end + + # When less data is returned than was requested, it means the end of the file with partial data. + @eof = true if block.size < @buffer_size if @buffer @buffer << block else # Take on the encoding from the input stream