【问题标题】:YAML data sequence problemYAML数据序列问题
【发布时间】:2009-09-29 15:58:47
【问题描述】:

解析 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}}

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

【问题讨论】:

    标签: ruby-on-rails ruby yaml


    【解决方案1】:

    您在这里构建的是地图而不是数组。据我所知,列表语法是:

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

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

    【讨论】:

    • 非常感谢。你说的对。必须学习更多 YAML 规范 :)
    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多