From: Robert Klemme Date: 2009-12-01T21:59:10+09:00 Subject: Re: regexing a file's contents without reading the whole thing? 2009/11/30 Roger Pack : > I see that it is possible currently to parse through a file without > reading the whole thing into RAM, a la > > a = File.open('a', 'r') > a.lines{|line| >  if line =~ /some regex/ >    ... >  end > } > > But what if I can to do something like > a = File.read('a').scan /some regex/ > > is that possible? If you know that matches will never cross line breaks you can do a = [] File.foreach("a") do |line| line.scan /regex/ do |m| a << m end # alternative: a.concat(line.scan(/regex/)) end If matches can cross line breaks the whole store becomes more complicated and your solution with File.read is probably the simplest way to do it (if files aren't too large). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/