Created
February 14, 2012 01:25
-
-
Save zenchild/1822307 to your computer and use it in GitHub Desktop.
Return the last line of a File in a O(n) time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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