YAML for Marshaling : YAML « XML « Ruby






YAML for Marshaling


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) 

 








Related examples in the same category

1.YAML Demo
2.use YAML::dump to convert your Person object array into YAML data
3.Output a hash with yaml
4.Reading and Writing Configuration Files
5.Convert hash to configuration file