Robert Klemme
9/27/2006 7:57:00 PM
Peter Bailey <pbailey@bna.com> wrote:
> unknown wrote:
>> On Thu, 28 Sep 2006, Peter Bailey wrote:
>>
>>> Well, I know that they're not always odd or even. They've been a
>>> mix of both. But, I understand what you're saying. I will change my
>>> original script. Basically, and, please tell me if I understand
>>> this correctly: if I'm going to do a scan of a file, open the file,
>>> scan it, and then close it. Right?
>>
>> yup. just remember to avoid this
>>
>> string = 'foobar'
>>
>> string.scan(%r/foo/) do |word|
>> string << 'foo' # can't modify while scanning
>> end
>>
>> regards.
>>
>> -a
>
> Thanks a lot, -a! I've cleaned up my code. But, if you notice way
> above, I've got a File.read in the line before the file scan. If I do
> an "end" for the file scan, my "read" is still open, right? Meaning,
> I can still do stuff to the open file.
If you're referring to your original code, then no. You use File.read(name)
which returns the whole file in a single string. No open connection is
returned.
Btw, for efficiency reasons if your files are large you might consider using
File.foreach(file_name) do |line|
....
end
Or use File.readlines instead of File.read - that way you get an array with
lines and not the whole file in one piece.
Kind regards
robert