【问题标题】:Modify an object before marshaling it in Ruby在 Ruby 中封送对象之前修改对象
【发布时间】:2016-08-01 18:14:12
【问题描述】:

我有一个包含敏感数据的对象,我想编组(使用Marshal没有敏感数据。

我想说:

def _dump(*args)
  # Clean-up sensitive data
  super
end

但这会产生“无超类方法”错误。有没有一种方法可以让我的对象按照我想要的方式响应Marshal.dump,同时使用默认实现?

我希望 Marshal.dump(my_obj) 开箱即用,无需 API 使用者记住调用不同的方法。

【问题讨论】:

  • 你能用marshal_dumpmarshal_load吗?

标签: ruby serialization marshalling dump superclass


【解决方案1】:

可能是_dump 没有超类方法。如果它是在你的对象上定义的,它就会被调用。如果不是,则使用默认处理程序。

您可能想要clone 您的对象并删除敏感字段,将其作为哈希返回到您的_dump 函数中,然后在_load 方法中撤消该操作。

您还可以阅读the documentation on Marshal,其中描述了推荐的方法。

【讨论】:

    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多