views:

96

answers:

1

Hello,

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 :(

+4  A: 

You're building a map not a array here. As fare as I do remember list syntax is:

columns:  
- col_1 : ~
- col_2 : ~
- col_3 : ~
- col_4 : ~

This will result in a map {"columns"=> [{"col_1"=>nil},{"col_2"=>nil}, {"col_3"=>nil}, {"col4"=>nil}] I suppose (did not test it).

Thomas Jung
Thank you very much. You are right. Have to study more YAML specs :)
oskarae
RTFM is always true. :-)
Thomas Jung