From: John Carter Date: 2006-09-28T08:40:27+09:00 Subject: Re: Idiomatic file snarf On Thu, 28 Sep 2006, James Edward Gray II wrote: > On Sep 27, 2006, at 5:02 PM, Tim Bray wrote: > >> I want to open a file, suck the contexts into a variable and close it >> again. I suppose I could IO#each and glue the lines together. >> >> Currently I have >> >> f = File.new(fname) >> text = f.read >> f.close >> >> Surely there's an idiomatic Ruby one-liner? > > Sure: > > text = File.read(fname) Actually, the idiom I most use is File.read( fname).scan( %r{ juicy stuff}x) do |match| # do something with juicy stuff end John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand "We have more to fear from The Bungling of the Incompetent Than from the Machinations of the Wicked." (source unknown)