开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜