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 file containing multiple objects.
+1
A:
There is a YAML class in Ruby core which has a short tutorial and a few links.
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
willcodejavaforfood
2010-10-05 11:25:43
I have seen this. But if I want the data from the YAML file into e.g. a class Zoo. Is there a way to map dogs, cats onto a attribute pretty_animals ?
poseid
2010-10-05 11:29:13
OK I'll update my post
willcodejavaforfood
2010-10-05 11:32:45
thanks, the 2nd reference gives the overview i was looking for. BTW reference kwiki seems to be out-of-service (?)
poseid
2010-10-05 12:10:38
@poseid - Oh sorry about that, I thought it was just my work blocking that site
willcodejavaforfood
2010-10-05 12:14:30