Skip to content

Instantly share code, notes, and snippets.

Created November 16, 2016 01:19
Show Gist options
  • Save anonymous/841f7594125243f3ea8e652524d073fa to your computer and use it in GitHub Desktop.
Save anonymous/841f7594125243f3ea8e652524d073fa to your computer and use it in GitHub Desktop.
buf = Bytes.new 4096
words = 1
lines = 0
bytes_total = 0
nbytes = 0
while (bytes = ARGF.read buf) > 0
bytes_total += bytes
buf.each do |c|
nbytes += 1
# this next line is what takes all the time
lines +=1 if c == 10
end
end
# why are nbytes and bytes_total different?
# it looks like buf.each keeps going after the end...
puts [lines, nbytes, bytes_total]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment