From: Tim Hunter Date: 2008-11-16T10:00:34+09:00 Subject: Re: IO#lineno= doesn't work the way I expected Jens Wille wrote: > hi chad! > > Chad Perrin [2008-11-16 01:32]: >> On Sun, Nov 16, 2008 at 05:42:18AM +0900, Tim Hunter wrote: >>> Chad Perrin wrote: >>>> 2. Is there a way to do what I actually need -- specifically, >>>> to iterate over an entire file *except* the first line, only >>>> reading one line at a time into RAM -- without writing a C >>>> extension for Ruby? >>> Why not iterate over the entire file and just ignore the first >>> line? IO#readline or IO#gets will get you a line at a time. >> How do you propose I "ignore" the first line? That's what I was >> trying to do -- by starting the iteration over lines in the file >> with the second line. Are you saying I should just have an >> orphan gets line in the code, then have a block in which I use >> gets each line until I run out of file? I guess that would >> probably work, but seems kinda . . . ugly. > not sure what tim was suggesting, but this will work: > > in_fh = File.open(infile, 'r') > out_fh = File.open(outfile, 'w') > > # ignore first line > in_fh.gets > > in_fh.each_line do |l| > out_fh << l > end > > ok, it's not pretty, but not *that* ugly either, is it? ;-) > > cheers > jens > > That's almost precisely what I was suggesting. For real code I'd want to handle the case of the file not having any lines, if that can occur. It doesn't look "ugly" to me at all. -- RMagick: http://rmagick.rubyforge.org/