lib/io_streams/line/reader.rb in iostreams-0.16.0 vs lib/io_streams/line/reader.rb in iostreams-0.16.1

- old
+ new

@@ -59,11 +59,11 @@ read_block # Auto-detect windows/linux line endings if not supplied. \n or \r\n @delimiter = delimiter || auto_detect_line_endings - unless eof? + if @buffer # Change the delimiters encoding to match that of the input stream @delimiter = @delimiter.encode(@buffer.encoding) @delimiter_size = @delimiter.size end end @@ -71,11 +71,14 @@ # 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 - yield(readline) until eof? + until eof? + line = readline + yield(line) unless line.nil? + end line_count end def readline return if eof? @@ -86,20 +89,21 @@ # Delimiter found? if index data = @buffer.slice(0, index) @buffer = @buffer.slice(index + @delimiter_size, @buffer.size) + @line_count += 1 elsif @eof && @buffer.empty? data = nil @buffer = nil else # Last line without delimiter data = @buffer @buffer = nil + @line_count += 1 end - @line_count += 1 data end # Returns whether the end of file has been reached for this stream def eof? @@ -128,11 +132,9 @@ # EOF reached? if block.nil? @eof = true return false - elsif block.size < @buffer_size - @eof = true end if @buffer @buffer << block else