0

解析 YAML 文件后,我需要在 Ruby 数组中具有正确的值顺序。

我有这个简单的例子来显示我的问题:

x = "columns:\n  col_1 : ~\n  col_2 : ~\n  col_3 : ~\n  col4 : ~"
s = YAML::load(x)

控制台输出给出:

x = "列:\n col_1:~\n col_2:~\n col_3:~\n col4:~"
=> "列:\n col_1:~\n col_2:~\n col_3:~\n col4: ~"
s = YAML::load(x)
=> {"columns"=>{"col_3"=>nil, "col4"=>nil, "col_1"=>nil, "col_2"=>nil}}

“列”数组的顺序与输入数据中的顺序不同:(

4

1 回答 1

4

你在这里建立一个地图而不是一个数组。据我所知,列表语法是:

columns:  
- col_1 : ~
- col_2 : ~
- col_3 : ~
- col_4 : ~

这将导致映射 {"columns"=> [{"col_1"=>nil},{"col_2"=>nil}, {"col_3"=>nil}, {"col4"=>nil}] I假设(没有测试它)。

于 2009-09-29T16:04:06.097 回答