2014-09-29 166 views
-1

我有一個用戶在puppet聲明中傳遞的散列,並且create_resources使用它,它需要添加一個新的嵌套級別並保留鍵成爲關鍵,但它的價值被設置爲一個嵌套的哈希與原始值和一些字符串的值。在ruby中修改一個哈希以使鍵的值包含嵌套哈希

等給出這樣

 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
hash.each{|k, v| hash[k] = {"ensure" => "present", "value" => v}} 
+1

這真是太棒了謝謝 – 2014-09-29 19:37:43