From: Robert Klemme Date: 2009-08-18T03:25:14+09:00 Subject: Re: while !file.eof? On 17.08.2009 17:39, Derek Smith wrote: > Robert Klemme wrote: >> 2009/8/17 botp : >> You're not closing the file handle properly. :-) >> >> Better: >> >> File.foreach "maillog" do |line| >> p line if /^#{last_record}/o =~ line >> end > OK thx guys! > > mailog="/tmp/maillog" > last_record="Jul 15 22:09:10" > File.open(mailog, 'r+').each { |line| > p line if line =~ /^#{last_record}/o > } > > But now, what is the diff between > > File.open( "maillog" ).each do |line| > and > File.open("mailog").each { |line| > > one better, or more ruby standard? I did not see a diff. > Personally I like "without the do" as there is less to type. The difference is just precedence. But actually both are bad idioms as they do not ensure that the File object is closed properly. File.foreach is better (see above). For a bit more background you can read on here http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/