22 lines
500 B
Ruby
22 lines
500 B
Ruby
|
class File
|
||
|
|
||
|
#read last n lines of a file. useful for getting last part of a big log file.
|
||
|
def tail(n)
|
||
|
buffer = 1024
|
||
|
idx = (size - buffer).abs
|
||
|
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/)
|
||
|
lines_to_return = ary[ ary.size - n, ary.size - 1 ]
|
||
|
end
|
||
|
end
|