Class: Racknga::ReverseLineReader — racknga - Ranguba

Class: Racknga::ReverseLineReader

Inherits:
Object
  • Object
show all
Defined in:
lib/racknga/reverse_line_reader.rb

Constant Summary

BYTES_PER_READ =
4096

Instance Method Summary (collapse)

Constructor Details

- (ReverseLineReader) initialize(io)

A new instance of ReverseLineReader



21
22
23
24
25
26
# File 'lib/racknga/reverse_line_reader.rb', line 21

def initialize(io)
  @io = io
  @io.seek(0, IO::SEEK_END)
  @buffer = ""
  @data = ""
end

Instance Method Details

- (Object) each {|@buffer| ... }

Yields:

  • (@buffer)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/racknga/reverse_line_reader.rb', line 28

def each
  separator = $/
  separator_length = separator.length
  while read_to_buffer
    loop do
      index = @buffer.rindex(separator, @buffer.length - 1 - separator_length)
      break if index.nil? or index.zero?
      last_line = @buffer.slice!((index + separator_length)..-1)
      yield(last_line)
    end
  end
  yield(@buffer) unless @buffer.empty?
end