-3

このハッシュを考えると:

myXML = {:_id=>BSON::ObjectId('51ad8d83a3d24b3b9f000001'),
         "Comment"=>nil,
         "Line"=>
          [{"LineNumber"=>"3.1",
            "Item"=>"fruit-004",
            "Description"=>"Peach",
            "Quantity"=>"1",
            "UnitCost"=>"1610",
            "DeclaredValue"=>"0",
            "PointValue"=>"13"},
           {"LineNumber"=>"8.1",
            "Item"=>"fruit-001",
            "Description"=>"Fruit Set",
            "Quantity"=>"1",
            "UnitCost"=>"23550",
            "PointValue"=>"105",
            "PickLine"=>
             [{"PickLineNumber"=>"8.1..1",
               "PickItem"=>"fruit-002",
               "PickDescription"=>"Apple",
               "PickQuantity"=>"1"},
              {"PickLineNumber"=>"8.1..2",
               "PickItem"=>"fruit-003",
               "PickDescription"=>"Orange",
               "PickQuantity"=>"2"}]}],
         "MemberId"=>"A00000001",
         "MemberName"=>"Bruce",
         "DeliveryId"=>"6377935",
         "ShipToAddress1"=>"123-4567",
         "OrderDate"=>"05/08/13",
         "Payments"=>
          [{"PayType"=>"Credit Card", "Amount"=>"1000"},
           {"PayType"=>"Points", "Amount"=>"5390"}]}

nilコードで値を持つ「コメント」キーを使用して、キーと値のペアを削除できます。

myXML.each do |key, value|
  myXML.delete(key) if myXML[key] == nil
end

Ruby でより少ないコードでこれを行うには、もっと良い方法があると思います。

4

1 に答える 1