Skip to content

Instantly share code, notes, and snippets.

@zenchild
Created February 14, 2012 01:25
Show Gist options
  • Save zenchild/1822307 to your computer and use it in GitHub Desktop.
Save zenchild/1822307 to your computer and use it in GitHub Desktop.
Return the last line of a File in a O(n) time
class File
def last_line
ind = 1
loop do
offset = (ind * -80)
if(offset >= size)
seek(0, IO::SEEK_SET)
line = read
line.chomp!
return line.split(/\n/, -1).last
else
seek(offset, IO::SEEK_END)
line = read
if(line =~ /\n.*\n/)
line.chomp!
return line.split(/\n/, -1).last
end
end
ind += 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment