Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to seek through a file without loading the whole thing into an array?

Tags:

ruby

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.

like image 870
Brian Avatar asked May 24 '13 07:05

Brian


1 Answers

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.

like image 142
vgoff Avatar answered Nov 15 '22 06:11

vgoff