Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/cpee-dstore/implementation.rb

Instance Method Summary collapse

Instance Method Details

#tail(n) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cpee-dstore/implementation.rb', line 24

def tail(n)
  buffer = 1024
  idx = [size - buffer, 0].max
  chunks = []
  lines = 0

  begin
    seek(idx)
    chunk = read(buffer)
    lines += chunk.count("\n")
    chunks.unshift chunk
    idx -= buffer
  end while lines < ( n + 1 ) && pos != 0

  tail_of_file = chunks.join('')
  ary = tail_of_file.split("\n")
  ary[ ary.size - n, ary.size - 1 ].join("\n")
end