-1

ハッシュの 2 つの配列があります。

[{:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}]

[{:status=>"success", :sender=>"ayfw-a"},
{:status=>"success", :sender=>"vpfw-a"}]

マージに使用できるキーは:node:senderです。:status次のように、2 番目の配列のパラメーターを として最初の配列に追加し、2 番目の配列に:another_status対応するパラメーターがない場所に「スキップ」を追加する必要があります:sender

[{:status=>"failed", :tag=>"tag156", :node=>"isfw-a", :another_status=>"skipped"},
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a", :another_status=>"success"},
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a", :another_status=>"success"}]

これを実装するための解決策が思いつきません。

4

3 に答える 3

-1
ar1 = [ {:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
        {:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
        {:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}]

ar2 = [{:status=>"success", :sender=>"ayfw-a"},
       {:status=>"success", :sender=>"vpfw-a"}]

new_hsh = ar1.map do |a1| 
  bol = ar2.select{|a2| a2[:sender] == a1[:node] }
  bol ? a1[:another_status]=bol[:status] : a1[:another_status]= 'skipped'
  a1
end

new_ary
# => [{:status=>"failed",
#      :tag=>"tag156",
#      :node=>"isfw-a",
#      :another_status=>"skipped"},
#     {:status=>"unchanged",
#      :tag=>"tag156",
#      :node=>"ayfw-a",
#      :another_status=>"success"},
#     {:status=>"changed",
#      :tag=>"tag156",
#      :node=>"vpfw-a",
#      :another_status=>"success"}]
于 2013-07-09T19:55:45.060 に答える
-1
a1 = [{:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}]

a2 = [{:status=>"success", :sender=>"ayfw-a"},
{:status=>"success", :sender=>"vpfw-a"}]

a1.map do |h1| h1.merge(another_status:
  a2.find{|h2| h2[:sender] == h1[:node]}.to_h[:status] || "skipped"
) end

出力

[
  {
    :status         => "failed",
    :tag            => "tag156",
    :node           => "isfw-a",
    :another_status => "skipped"
  },
  {
    :status         => "unchanged",
    :tag            => "tag156",
    :node           => "ayfw-a",
    :another_status => "success"
  },
  {
    :status         => "changed",
    :tag            => "tag156",
    :node           => "vpfw-a",
    :another_status => "success"
  }
]
于 2013-07-09T19:02:54.383 に答える