From: Robert Klemme Date: 2004-04-26T17:44:04+09:00 Subject: Re: [NEWBIE] Extract all occurences from a text "David Alan Black" schrieb im Newsbeitrag news:m3ekqeu4le.fsf@wobblini.net... > Hi -- > > Kristof Bastiaensen writes: > > > On Fri, 23 Apr 2004 05:08:00 -0700, David Alan Black wrote: > > > > > Or even more simply: > > > > > > lines = open(filename).grep(regex) > > > > > > > doesn't that leave the file open? > > Sigh -- yes. That's a recurrent mental glitch I've got. File#readlines does the trick correctly: lines = File.readlines(filename).grep(regexp) Only drawback: there's an intermediate array which burns a lot of mem if the file is huge. In those cases better do: lines = File.open(filenam) do |io| l=[] while ( line = io.gets ) l << line if regexp =~ line end l end Regards robert