Class: My::ReadLine

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/my/read_line.rb

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, &block) ⇒ ReadLine

Returns a new instance of ReadLine.



6
7
8
# File 'lib/my/read_line.rb', line 6

def initialize input = nil, &block
  @input = input || Enumerator.new(&block)
end

Instance Method Details

#each {|last_line| ... } ⇒ Object

Yields:

  • (last_line)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/my/read_line.rb', line 10

def each
  last_line = ''
  @input.each do |chunk|
    chunk.each_line do |line|
      if line[-1] == "\n"
        unless last_line.empty?
          last_line << line
          line, last_line = last_line, ''
        end
        yield line
      else
        last_line << line
      end
    end
  end
  yield last_line unless last_line.empty?
end