From: Brian Candler Date: 2010-03-28T20:47:25+09:00 Subject: Re: Help to get value from abject inside an array Nicolas 2b wrote: > My problem use RoR object but is ruby oriented so I post here. > I got an array with lot of objects (type 'Product') like this (only 4 > here) : > > [#, # 2>, #, # company_id: 4>] > > What I want to do is iterate on the array and for each company_id, know > how much products have the same company_id. > I get the company_id like this : products.collect(&company_id).uniq > > but after I don't know how to get an array like this : [1 => 22, 2 => > 15, 3 => 9] company_products = Hash.new(0) products.each { |p| company_products[p.company_id] += 1 } p company -- Posted via http://www.ruby-forum.com/.