From: Joel VanderWerf Date: 2007-08-02T15:19:22+09:00 Subject: Re: ordered hashes/ yaml? d c wrote: > i understand hashes dont keep their order in ruby, so wondering what the > most elegant way to create some YAML that i want to maintain order is... You can monkey-patch Hash to do it: class Hash def to_yaml( opts = {} ) YAML::quick_emit( object_id, opts ) do |out| out.map( taguri, to_yaml_style ) do |map| sorted_keys = keys sorted_keys = begin sorted_keys.sort rescue sorted_keys.sort_by {|k| k.to_s} rescue sorted_keys end sorted_keys.each do |k| map.add( k, fetch(k) ) end end end end end -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407