【问题标题】:Skipping chosen types in ruby yaml serialisation在 ruby​​ yaml 序列化中跳过选择的类型
【发布时间】:2010-11-15 12:09:34
【问题描述】:

我正在使用YAML::dump() 序列化一些对象。不幸的是,这包括一些不应该被序列化的元素,比如有时带有等待线程的锁。

有没有办法从序列化中排除选定的类型,或者强制将它们序列化为空对象?

【问题讨论】:

    标签: ruby class serialization yaml


    【解决方案1】:

    解决方法:

    class ClassToNil
      def to_yaml
        nil.to_yaml
      end
    end
    

    【讨论】:

      【解决方案2】:

      对我来说这听起来不太优雅,但你可以将这些对象扩展为 nil 转储

      class IgnoredObject
        def to_yaml
          nil
        end
      end
      

      【讨论】:

      • to_yaml': wrong number of arguments (1 for 0) (ArgumentError); then after fixing that -- emit': 参数类型错误 nil (expected Data) (TypeError)
      猜你喜欢
      • 2012-09-09
      • 2011-10-16
      • 2013-08-21
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 2017-03-09
      相关资源
      最近更新 更多