From: "David A. Black" Date: 2008-08-20T21:56:29+09:00 Subject: Re: array of hashes - need to iterate and calulate stats but how Hi -- On Wed, 20 Aug 2008, Lex Williams wrote: > David A. Black wrote: >> Hi -- >> >> On Wed, 20 Aug 2008, Lex Williams wrote: >> >>> end >>> end >>> end >>> >>> and then you would have a statistic_hash containing as key a product >>> name , and as value , the number of units sold. I'm not sure about how >>> to calculate the statistics . >> >> You can use a hash with a default value to make all that hash testing >> easier: >> >> statistic_hash = Hash.new(0) >> array.each do |hash| >> next unless hash[:did_it_sell] >> statistic_hash[hash[:title]] += 1 >> end >> >> >> David > > you could use select on the array , and lose those if's . something like > this : > > array.select { |hash| hash[:did_it_sell] }.each do |hash| > statistic_hash[hash[:title]] += 1 > end To answer my own question: I guess you do mean the 'unless'. I deliberately didn't use select just to save the creation of an intermediate object, and because I think that the 'next unless' line is nice and expressive. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL See http://www.rubypal.com for details and updates!