For some reason, I cannot find any tutorial that mentions how to do this ... So, how do I read the first n lines from a file?
I figured it out:
while File.open('file.txt') and count <= 3 do |f| ... count += 1 end end
but it does not work, and it also does not look very good to me.
Just out of curiosity, I tried things like:
File.open('file.txt').10.times do |f|
but that didn't work either.
So, is there an easy way to read only the first n lines without loading the whole file? Thank you very much!
source share