与默认proc元帅红宝石哈希 - 删除默认proc? [英] Marshal ruby hash with default proc - remove the default proc?
问题描述
我有一个默认proc的Hash,我想将它编组为一个文件,但默认proc阻止我这样做。
I've got a Hash with a default proc that I'd like to Marshal to a file, but the default proc prevents me from doing that.
相反比写我自己的 _dump
和 _load
方法,是否有可能改为删除默认的proc?在编组的地方,我再也不需要默认的proc了。
Rather than writing my own _dump
and _load
methods, is it possible instead to remove the default proc instead? At the point where I'm Marshalling I'm never going to need the default proc again.
推荐答案
只需重新设置默认值:
Just reset the default:
h.default = nil
更明确地说:
More explicitly:
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中,您 也可以编写如果你愿意, h.default_proc = nil
。适用于所有使用的红宝石需要'backports / 2.0.0 / hash / default_proc'
。
In Ruby 2.0, you can also write h.default_proc = nil
if you prefer. Available for all Rubies with require 'backports/2.0.0/hash/default_proc'
.
这篇关于与默认proc元帅红宝石哈希 - 删除默认proc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!