Aha! A new YAML.rb is out at long last: >
YAML is a structured data format. You may have used Wiki? Well, Wiki is
a powerful alternative to HTML. In many ways, YAML is an alternative to
XML. You can represent almost any Ruby object in YAML.
Some good places to start learning are:
- The YAML Cookbook: http://yaml4r.sf.net/cookbook/
- YAML.rb now has CHM help: http://prdownloads.sourceforge.net/yaml4r/yamlrb-0.39.chm
- “Look Ma, No Tags”: http://www.xml.com/pub/a/2002/07/24/yaml.html
About the new release: >
The new YAML.rb 0.40 now handles references in emitting (including circular
references). This release also introduces numerous enhancements and fixes
to the Emitter class. Thanks to Tom Sawyer for a number of bug reports.
The latest release also adds convenient methods for adding your own custom
to_yaml methods to your classes. For example, if you have an object and
you want to organize its properties in a specific order:
class MouseMan
attr_accessor :name, :cape, :length_of_tail
def to_yaml( opts = {} )
YAML::quick_emit( self.id, opts ) { |out|
out.map( "!ruby/object:MouseMan" ) { |map|
[:name, :cape, :length_of_tail].each { |p|
map.add( p.id2name, self.send( p ) )
}
}
}
end
end
mm = MouseMan.new
mm.name = "Zipper"
mm.cape = true
mm.length_of_tail = "1.45 cm"
puts mm.to_yaml
# (prints)
# --- !ruby/object:MouseMan
# name: Zipper
# cape: (true)
# length_of_tail: 1.45 cm
So, there are basically Emitter#map and Emitter#seq methods for
controlling how you output those constructs.
This document is also valid YAML!: >
Try copying and pasting it into IRB, like:
txt =<<EOY
# .. paste in here ..
EOY
ann = YAML::load( txt )
Then, access the ‘ann’ variable. Should be a Ruby Hash!
YAML.rb is available at http://yaml4r.sf.net/
_why