Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/tailog/ext/file.rb
Instance Method Summary collapse
Instance Method Details
#tail(n) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/tailog/ext/file.rb', line 2 def tail(n) buffer = 1024 idx = size > buffer ? size - buffer : 0 chunks = [] lines = 0 begin seek(idx) chunk = read(buffer) break unless chunk lines += chunk.count("\n") chunks.unshift chunk idx -= buffer end while lines < ( n + 1 ) && idx >= 0 chunks.join('').split(/\n/).last(n) end |