修改ruby中的散列以使键的值包含嵌套的散列

时间:2014-09-29 06:22:07

标签: ruby arrays hash nested puppet

我有一个用户在puppet声明中传递的哈希,并且create_resources使用它,需要添加一个新的嵌套级别,其中键仍然是键,但它的值被设置为a嵌套哈希,其中包含原始值和一些字符串的值。

喜欢这个

      hash = {
            "Field1" => "Value11",
            "Field2" => ["value1","value2"],
      }

然后希望在此之后将新哈希作为此输出

      hash = {
           "Field1" => { "ensure" => "present",
                          "value"  => "Value11",
                        },
           "Field2" => { "ensure" => "present",
                          "value" => ["value1","value2"],
                       },
            }

试图做

           added = {'ensure' => 'present'}
           hash.zip([added])

无济于事。

1 个答案:

答案 0 :(得分:1)

hash.each{|k, v| hash[k] = {"ensure" => "present", "value" => v}}