# Create Doc By Yanglei, 2010-12-16
#
# > 的作用,以缩进对齐来判断是否为一段文字,也就是说,一旦缩进与上一行不一致,则认为是一个新行。
# node1的例子中,第一行“Ther... door”,
# 第二行“ "Please... floor"”,
# 第三行“So...So2”
node1: >
Ther once was a man from Darjeeling
Who got on a bus bound for Ealing
It said on the door
"Please don't spit on the floor"
So he carefully spat on the ceiling
So2
# | 的作用,它表示之后的文字,每一行均为一个新行。
node2: |
Ther once was a man from Darjeeling
Who got on a bus bound for Ealing
It said on the door
"Please don't spit on the floor"
So he carefully spat on the ceiling
# & 的作用,它表示一个“锚点标记”,其它节点可以使用“*”或“<<: *”来引用它的值
node3: &node3
a: 001
b: 002
# * 的作用,指node4的内容与node3完全一致
node4:
*node3
# <<: * 的作用,指node5的内容包含但不完全相同于node3的值。
node5:
<<: *node3
c: 003
# !! 的作用,强迫转换类型。
#输出:
#{"node6"=>{
# "a"=>#<YAML::PrivateType:0x9df6d40 @value="123", @type_id="float">,
# "b"=>#<YAML::PrivateType:0x9df6ae8 @value="true", @type_id="str">,
# "c"=>true
#}
#注意:c的值为布尔型。
node6:
a: !!float 123
b: !!str true
c: True
# 二进制内容的表示
node7: !!binary |
xxxxxxxxxxxxx
xxxxxxxxx
xxxxx
node8_value: &node8_value {id: 10000, code: item_manager, name: 项目经理}
#自定解析类型,YAML某Key的Value一般为Array或Hash,但如果需要将Value解析为其它的自定义类型,可以使用该方法。
#步骤:
# 1、首先定义 MyCustClass 类,如:
# class MyCustClass
# attr_accessor :id
# attr_accessor :code
# def initialize v_hash
# @id = v_hash["id"]
# @code = v_hash["code"]
# end
# end
# 2、向YAML注册解释类型,如:
# YAML::add_domain_type("yaml.org,2002", 'MyCustClass') do |type, val|
# MyCustClass.new(val)
# end
# 3、OK,当YAML文件加载时,YAML将自动将“node8”的值解析为MyCustClass类型。
# 4、测试一下,x["node8"] >> #<MyCustClass:0x9df1c88 @code="item_manager", @id=10000>
# x["node8"].code >> "item_manager"
node8: !MyCustClass
<<: *node8_value
# ? 的作用,用来明确的表示多个词汇组成的键值
# a["node9"] => {{"a"=>1, "b"=>2}=>[1, 2], "c"=>3}
node9:
? {a: 01, b: 02}
: [1, 2]
c: 3