「Ruby入門」RubyのYAMLオブジェクトのシリアル化

rubyコード:
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(“こんにちは", “there", “World")
puts “Before: obj = #{obj}"
data = YAML.dump(obj)
obj = YAML.load(data)
puts “After: obj = #{obj}"

obj = Special.new(“こんにちは", “there", “World")
puts YAML.dump(obj)

Development

Posted by arkgame