On 18/11/05, Belorion <belorion / gmail.com> wrote:
> >
> > If remember that the docn says that lineno= just changes the current
> > value of lineno -- if you check the result of f.lineno after your
> > f.readline above, you'll get 5. Not sure how useful that behavior is,
> > but that's how it's defined....
>
>
> I noticed that ... but, what, exactly is that useful for? (as you already
> questioned) It seems like I am missing something here, because otherwise
> lineno=() seems useless and misleading.
>

It could be used if you read into a file and want to update lineno
manually. E.g.

File.open("f") do | f |
  header = f.read(1024)
  f.lineno = header.gsub(/[^\n]/, "").length
  do_something_with_f_that_needs_linenumbers(f)
end

>
> Suggest that you read the file into memory and split it by lines
> > (File#readlines IIRC, not near the manual now).
> >
>
> The only problem with that is I need to query, say, only 1000 lines in a
> file with 195_199_572 lines in it.
>
>

Are the lines you need known at once? Then you could do it like this:

lines = [1, 2, 4, 8, 16, 32, 1024]
lines = lines.sort.reverse
File.open("file") do | f |
  while line = f.gets
    if line == lines.first
      puts line
      lines.pop
    end
  end
end

Brian


--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/