"Robert Dober" <robert.dober / gmail.com> writes:

> On 7/10/07, John Carter <john.carter / tait.co.nz> wrote:
>> Just got the O'Reilly announcement of the book "Beautiful
> <snip>
>> That makes it real pretty code.
>
> Save for
>
>   File.open(some_name).each_line
>
> Let the GC close the file!? It is not going to happen !!

Good point.  In many ways, it feels that the author is not as
comfortable with Ruby blocks as one should be.  A better version,
since we also don't need a separate class:

def get_big_hash(filename)
  hash = Hash.new {|h,k| h[k]=[]}
  lines = 0
  File.open(filename) do |file|
    file.each_line do |line|
      s = line.split
      article = s[2].intern
      hash[s[0]] << [ s[1], article ]
      lines += 1
      STDERR.puts "Line: #{lines}" if (lines % 100_000) == 0
    end
  end
  return hash
end

I really like the block-using form of File.open.  It makes me feel all
nice and safe like being wrapped in with-open-file or a carefully
constructed dynamic-wind.

-- 
s=%q(  Daniel Martin -- martin / snowplow.org
       puts "s=%q(#{s})",s.to_a.last       )
       puts "s=%q(#{s})",s.to_a.last