This works:
f = File.new("myfile").readlines
f[0] #=> "line 1"
f[21] #=> "line 22"
But what if I have a very large file, and only need to read a few lines. Is it possible to seek to specific lines and read them in Ruby, without loading the file into an array?
I grok IO streams, where (as in the case of stdin) you can't randomly seek through a stream. Surely there must be a way to do this without loading the entire file.
Don't ignore the IO
class. IO::foreach
is one of those methods that returns an Enumerator, and can be lazily evaluated.
IO#each_line
is also another one that will return an Enumerator.
In Ruby 2.0 we can call .lazy
and use those methods, except for zip and cycle, that allow us to traverse the enumeration without bringing the whole file into memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With