Module: IOTail

Included in:
IO, StringIO
Defined in:
lib/io_tail.rb

Overview

IOTail provides a tail_lines method as a mixin. By default it is included into IO and StringIO, if present. If you require StringIO after IOTail, then simply open StringIO and include IOTail.

Instance Method Summary collapse

Instance Method Details

#tail_lines(&block) ⇒ Object

Jumps to near the end of the IO, then yields each line, waiting for new lines if it reaches eof?



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/io_tail.rb', line 12

def tail_lines(&block) # :yields: line
  self.seek(-1, IO::SEEK_END)
  self.gets

  loop do
    self.each_line(&block)

    if self.eof? then
      sleep 0.25
      self.pos = self.pos # reset eof?
    end
  end
end