我需要定义用于序列化/反序列化对象的自定义方法。我想做如下的事情。
class Person
def to_yaml_type
"!example.com,2010-11-30/Person"
end
def to_yaml
"string representing person"
end
def from_yaml(yaml)
Person.load_from(yaml)
end
end
声明序列化/反序列化的正确方法是什么?
好的,这就是我的想法
class Person
def to_yaml_type
"!example.com,2010-11-30/person"
end
def to_yaml(opts = {})
YAML.quick_emit( nil, opts ) { |out|
out.scalar( taguri, to_string_representation, :plain )
}
end
def to_string_representation
...
end
def Person.from_string_representation(string_representation)
... # returns a Person
end
end
YAML::add_domain_type("example.com,2010-11-30", "person") do |type, val|
Person.from_string_representation(val)
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)