Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selective file read in Ruby

Tags:

ruby

seek

I have a huge file that looks like this:

7

bla1
blala
blabla
blab
blals
blable
bla

more here..

The first numbers tells how many values I will have. The thing, is that i just want to point directly to the line 11 (text "more here.."), without having to read all those values before. In my case, I have a big amount of numbers, so it has to be optimized.

Would you recommend me something?

like image 807
frarees Avatar asked Dec 08 '25 02:12

frarees


1 Answers

You could probably use File#seek to randomly access the file.

The problem with that approach is that it will just access data at a specified byte offset - not a line offset. If your file could give the byte offset at the start of the file to where the list finishes, then you could use that.

like image 120
madlep Avatar answered Dec 10 '25 17:12

madlep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!