6

orders、idx、account などの複数のキーを group_by したいと思います。以下のコードは、Ruby on Rails の修正バージョンです - 配列のハッシュ、group by および sum by column name。複数のキーをグループ化し、複数の値を合計する方法を推奨できる人はいますか?

たとえば、次のコードでは、「注文」のみをグループ化しています。注文、idx、およびアカウントでグループ化したいと考えています。

group_hashes some_array, ["order","idx","account"] ["money","amt"]

対。

group_hashes some_array, "order", "money","amt" 

コード:

some_array=[{"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", 
      "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde",  "amt"=>"2" ,
      "money"=>"2.00", "order"=>"00001"}, {"idx"=>"1235", 
      "account"=>"abde", "amt"=>"2" , "money"=>"3.00", "order"=>"00002"}]  # => [{"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde", "amt"=>"2", "money"=>"2.00", "order"=>"00001"}, {"idx"=>"1235", "account"...

#group_hashes arr, "order", "money"
def group_hashes arr, group_field, *sum_field
  arr.inject({}) do |res, h|
    (res[h[group_field]] ||= {}).merge!(h) do |key, oldval, newval|
        sum_field.include?(key) ? (oldval.to_f + newval.to_f).to_s : oldval  # => "1234", "abde", "4.0", "6.0", "00001"
    end                                                                      # => {"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}, {"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}, {"idx"=>"1235", "account"...
    res                                                                      # => {"00001"=>{"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}}, {"00001"=>{"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}}, {"0...
  end.values                                                                 # => [{"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde", "amt"=>"2", "money"=>"3.00", "order"=>"00002"}]
end                                                                          # => nil

group_hashes some_array, "order", "money","amt"  # => [{"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde", "amt"=>"2", "money"=>"3.00", "order"=>"00002"}]

入力からの期待される出力:

input =[  {"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}, 
          {"idx"=>"1234", "account"=>"abde",  "amt"=>"2" ,"money"=>"2.00", "order"=>"00001"}, 
          {"idx"=>"1235", "account"=>"abde", "amt"=>"2" , "money"=>"3.00", "order"=>"00002"},
          {"idx"=>"1235", "account"=>"abde", "amt"=>"3", "money"=>"4.00", "order"=>"00002"},
          {"idx"=>"1234", "account"=>"ddd", "amt"=>"2", "money"=>"4.00", "order"=>"00003"},
          {"idx"=>"1234", "account"=>"ddd", "amt"=>"2", "money"=>"4.00", "order"=>"00003"}
        ]
output =[  {"idx"=>"1234", "account"=>"abde", "amt"=>"4", "money"=>"6.00", "order"=>"00001"}, 
          {"idx"=>"1235", "account"=>"abde", "amt"=>"5" , "money"=>"7.00", "order"=>"00002"},
          {"idx"=>"1234", "account"=>"ddd", "amt"=>"4", "money"=>"8.00", "order"=>"00003"},
        ] 
4

1 に答える 1