Skip to content

Instantly share code, notes, and snippets.

@manveru
Created September 17, 2010 09:19
Show Gist options
  • Save manveru/583961 to your computer and use it in GitHub Desktop.
Save manveru/583961 to your computer and use it in GitHub Desktop.
class 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 && pos != 0
chunks.join.lines.reverse_each.take(n).reverse.join
end
end
File.open('rpn-calculator.rb') do |f|
p f.tail(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment