与默认proc元帅红宝石哈希 - 删除默认proc? [英] Marshal ruby hash with default proc - remove the default proc?

查看:127
本文介绍了与默认proc元帅红宝石哈希 - 删除默认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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆