Getting YAML elements out in the file order

D

deane.barker

I have a YAML file which I get into Ruby using the "load" method of the
YAML class.

I would like Ruby to respect the order of elements in the YAML file, so
I could get them out of the object in the same order they appear in the
file.

Is this possible? Ruby puts the elements into a Hash, which famously
doesn't promise any ordering when the elements come out, so I'm not
optimistic.

So, is it possible to get elements out of a YAML object in Ruby in the
same order that they appear in the file, or should I move on to another
way of doing it?

Deane
 
D

Dominik Werder

why not using a separate array which contains the same elements as the
hash but in the order of your choice? YAML will store the data only once
and maintains a reference list for the array, so your file will not be
much bigger in size.
Just try it and see if it meets your taste..

bye!
Dominik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top