Ruby 的 YAML 对象序列化

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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 
        "#@valuable #@volatile #@precious" 
    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)