Loading and generating Yaml file with similar keys

Viewed 1525

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']))

I'm using "ya2yaml" gem for generating yaml file. In order to get the same order as it's in sample_file.yml i've used hash_order here @new_file.syswrite(@file.ya2yaml(:hash_order => ['title','department','input_formats'])), but it's not working. How can i retain the order?

3 Answers
Related