From: Jacob Fugal Date: 2006-02-03T08:10:04+09:00 Subject: Re: Seeking the Ruby way On 2/2/06, ara.t.howard@noaa.gov wrote: > require "csv" > require "yaml" > > path = ARGV.shift > sum = Hash::new{|h,k| h[k] = 0} > count = lambda{|row| sum[row.last.to_s[0,6]] += 1} > CSV::open(path,"r"){|row| count[row]} > y sum.delete_if{|k,v| v == 1} I'm curious why you decided to make `count` its own lambda when: 1) It's only ever used once 2) The block that uses it has only one statement, namely the call to `count` 3) count and the block to CSV::open have the same signature I think at a minimum, given 2) and 3), I'd just replace the block to CSV::open with count itself: count = lambda{|row| sum[row.last.to_s[0,6]] += 1} CSV::open(path,"r", &count) Then, since count isn't used anywhere else, I'd join those together: CSV::open(path,"r"){|row| sum[row.last.to_s[0,6]] += 1} After those transformations: galadriel:~ lukfugl$ cat a.rb require "csv" require "yaml" path = ARGV.shift sum = Hash::new{|h,k| h[k] = 0} CSV::open(path,"r"){|row| sum[row.last.to_s[0,6]] += 1} y sum.delete_if{|k,v| v == 1} galadriel:~ lukfugl$ cat in.csv 0,aaaaaa___ 1,aaaaaa___ 2,aaabbb___ 3,aaabbb___ 4,aaabbb___ 5,aaaccc___ galadriel:~ lukfugl$ ruby a.rb in.csv --- aaaaaa: 2 aaabbb: 3 Just seems a little clearer to me over having an extra one-time use lambda. Jacob Fugal