Skip to content

Commit fbe0b4e

Browse files
committed
Add each_line enumerator to IO class
1 parent 599bfd4 commit fbe0b4e

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

lib/piperator/io.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def gets(separator = $INPUT_RECORD_SEPARATOR, _limit = nil)
4949
read_with { @buffer.gets(separator) }
5050
end
5151

52+
# Returns an enumerator of lines in the stream, without reading the entire stream into memory
53+
#
54+
# @return [Enumerator]
55+
def each_line
56+
Enumerator.new { |y| loop { y << gets } }.lazy.take_while(&:itself).each
57+
end
58+
5259
# Flush internal buffer until the last unread byte
5360
def flush
5461
if @buffer.pos == @buffer_read_pos

0 commit comments

Comments
 (0)