YAML数据顺序问题
解析 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)
=> {“列”=>{“col_3”=>nil,“col4”=>nil,“col_1”=>nil,“col_2”=>nil}}
columns" 数组的顺序不同它在输入数据中:(
I need to have correct order of values inside Ruby array after parsing YAML file.
I have this simple example showing my issue:
x = "columns:\n col_1 : ~\n col_2 : ~\n col_3 : ~\n col4 : ~"
s = YAML::load(x)
console output gives :
x = "columns:\n col_1 : ~\n col_2 : ~\n col_3 : ~\n col4 : ~"
=> "columns:\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" array is in different sequence as it was in input data :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里构建的是地图而不是数组。据我所知,列表语法是:
这将生成一个映射 {"columns"=>; [{"col_1"=>nil},{"col_2"=>nil}, {"col_3"=>nil}, {"col4"=>nil}] 我想(没有测试)。
You're building a map not a array here. As fare as I do remember list syntax is:
This will result in a map {"columns"=> [{"col_1"=>nil},{"col_2"=>nil}, {"col_3"=>nil}, {"col4"=>nil}] I suppose (did not test it).