My task is to express a YAML structure like the following from ruby data structures:
- a
- b
- a
- include:
name: test
- include:
name: test2
I tried the following:
require "json"
#=> true
require "yaml"
#=> true
array = ["a","b","a","include" => {"name"=>"test"}]
#=> ["a", "b", "a", {"include"=>{"name"=>"test"}}]
puts JSON.parse(array.to_json).to_yaml
#---
#- a
#- b
#- a
#- include:
# name: test
#=> nil
So this looks like I'm on the right track. But when I simply add another hash entry into the array I get the following:
array = ["a","b","a","include" => {"name"=>"test"}, "include" => {"name"=>"test2"}]
#(irb):23: warning: duplicated key at line 23 ignored: "include"
#=> ["a", "b", "a", {"include"=>{"name"=>"test2"}}]
puts JSON.parse(array.to_json).to_yaml
#---
#- a
#- b
#- a
#- include:
# name: test2
#=> nil
This confuses me a lot. Shouldn't the entries of an array be independent of each other? Why does Ruby merge the last two entries together to one hash? And what do I have to do to create the given YAML structure with ruby data structures, if that is possible at all (I'm sorry if that is a stupid question, but I'm a Ruby beginner)?
includetwice.