2010-09-29 94 views

回答

14

只是重置默認:

h.default = nil 

更明確地說:

def dumpable_hash(h) 
    return h unless h.default_proc 
    copy = h.clone 
    copy.default = nil # clear the default_proc 
    copy 
end 

在Ruby 2.0,你也canh.default_proc = nil如果你喜歡。適用於require 'backports/2.0.0/hash/default_proc'的所有紅寶石。

+1

原來'h.default = nil'清除默認PROC - 這就是我需要的。謝謝! – 2010-09-29 04:30:17

+0

也可以轉儲具有默認值對象的散列,例如'Hash.new(42)'。並不是說我喜歡擁有默認值對象,除非它們是不可變的。 – 2011-06-30 23:19:37

5

如果你想有一個副本,不違約,最簡單的方法 -

Hash[hash_with_defaults]