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.
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
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'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With