Ruby 的 YAML 对象序列化

2019PHP高薪工程师学习路线图....>>>

require 'yaml' 
class Special 
    def initialize(valuable, volatile, precious) 
        @valuable = valuable 
        @volatile = volatile 
        @precious = precious 
    end 

    def to_yaml_properties 
        %w{ @precious @valuable } 
    end 
    def to_s 
        "[email protected] [email protected] [email protected]" 
    end 
end 

obj = Special.new("Hello", "there", "World") 
puts "Before: obj = #{obj}" 
data = YAML.dump(obj) 
obj = YAML.load(data) 
puts "After: obj = #{obj}" 

obj = Special.new("Hello", "there", "World") 
puts YAML.dump(obj)