What are good examples of mapping YAML data to Ruby objects?
I am looking for basic examples of YAML syntax and how to work with it in Ruby. Basically, by looking at the examples, I hope to better understand how to map YAML scalars to object attributes, and whether to use different YAML files or having one YAML fil开发者_StackOverflow社区e containing multiple objects.
There is a YAML class in Ruby core which has a short tutorial and a few links.
YAML in Five Minutes
Serializing and Deserializing objects with Ruby
require "yaml"
test_obj = ["dogs", "cats", "badgers"]
yaml_obj = YAML::dump( test_obj )
# -> ---
- dogs
- cats
- badgers
ruby_obj = YAML::load( yaml_obj )
# => ["dogs", "cats", "badgers"]
ruby_obj == test_obj
# => true
精彩评论