From: Sam Dela Cruz Date: 2005-12-10T06:08:12+09:00 Subject: Re: new to Ruby - pls help in translating this --=_alternative 007417F5882570D2_= Content-Type: text/plain; charset="US-ASCII" Yes, file handles must be closed. I learned that the hard way on one of my projects before. Thanks again. Sam mental@rydia.net 12/09/2005 01:02 PM To Sam Dela Cruz/SVL/SC/PHILIPS@PHILIPS cc Subject Re: new to Ruby - pls help in translating this Classification Quoting Sam Dela Cruz : > Here's what I decided on using (looks more like what I originally > posted in perl): > > hash = Hash.new(0) > File.open(ARGV[0],"r").each do |line| > hash[line.chomp] += 1 > end > > hash.keys.sort.each do |key| > puts "#{key}: #{hash[key]}" if hash[key] > 1 > end One caution here -- if you want the file handle to be closed when you are finished, you must do either: stream = File.open(ARGV[0],"r") stream.each do |line| hash[line.chomp] += 1 end stream.close or (this is often better because it will automatically close the handle even if there's an exception): File.open(ARGV[0],"r") do |stream| stream.each do |line| hash[line.chomp] += 1 end end -mental --=_alternative 007417F5882570D2_=--