2012-03-26 28 views
-4

的陣列值求和我給對象如何從對象

array = [rep_type: "working", time_spend: "325", ....;rep_type: "extra", time_spend: "100", ..... ;...] 

我需要計算特定的「rep_typ」 S我怎麼能做到這一點總和的陣列? 例如: 「工作」 - 500 「額外」 - 200

+5

這是什麼語言? – 2012-03-26 17:26:58

+0

似乎是紅寶石。但它不是有效的符號... – 2012-03-26 17:28:14

+0

是的,它是紅寶石,其示意圖 – user1136228 2012-03-26 18:14:11

回答

1
result = Hash.new(0) 
a.each { |o| 
    result[o.rep_type] += o.time_spend 
}