I’ve got a yaml file document which have similar keys :-
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
After generating new_file.yml the keys get sorted in alphabetical order :-
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
the code for opening sample_file and making new_file is below :-
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']))
Finally i got the solution of the ordering issue.
The :hash_order only work for top level hash.
so it works only when i remove “line” key from my sample_file.yml. Then the order is preserved. :-