Man*_*Das 9 ruby yaml rubygems ruby-on-rails ruby-on-rails-3
我有一个yaml文件文件有类似的键: -
line:
title: line-name
department: transcription
input_formats:
- input_format:
name: company
required: true
valid_type: general
- input_format:
name: website
required: false
valid_type: url
Run Code Online (Sandbox Code Playgroud)
生成new_file.yml后,键按字母顺序排序: -
line:
department: transcription
input_formats:
-
input_format:
name: company
required: true
valid_type: general
-
input_format:
name: website
required: false
valid_type: url
title: line-name
Run Code Online (Sandbox Code Playgroud)
打开sample_file并生成new_file的代码如下: -
require 'yaml'
require 'ya2yaml'
@file = YAML::load(File.open("/Users/manish/Desktop/yaml/sample_file.yml"))
@new_file = File.new("/Users/manish/Desktop/yaml/new_file.yml", "w+")
@new_file.syswrite(@file.ya2yaml(:hash_order => ['title','department','input_formats']))
Run Code Online (Sandbox Code Playgroud)
@new_file.syswrite(@file.ya2yaml(:hash_order => ['title','department','input_formats'])),但它不起作用.我如何保留订单?最后我得到了订购问题的解决方案.
:hash_order仅适用于顶级哈希.
所以只有当我从sample_file.yml中删除"line"键时它才有用.然后保留订单.: -
title: line-name
department: transcription
input_formats:
- input_format:
name: company
required: true
valid_type: general
- input_format:
name: website
required: false
valid_type: url
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1420 次 |
| 最近记录: |