[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML extension not honouring the options passed to .to_yaml

Builder Chad

12/9/2006 2:40:00 AM


As outlined on http://yaml4r.sourceforg... I triesd setting some
options in the hash passed to .to_yaml and nothing seems to be making
any difference:


out_file.puts newmaster.to_yaml(:SortKeys => true)

newmaster is a hash and the output is definitely not being sorted.
Unfortunately using newmaster.sort.to_yaml changes the structure of the
yaml being output significantly (seeing as it is converted to and array
of arrrays) and that is not desirable). Can anyone shed some light on
this? I am using vanilla 1.8.4 install on windows and presumably the
YAML.rb that shipped with that because I haven't installed anything
separately.

Cheers.

--
Posted via http://www.ruby-....

2 Answers

Joel VanderWerf

12/9/2006 2:57:00 AM

0

Chad Thatcher wrote:
> As outlined on http://yaml4r.sourceforg... I triesd setting some
> options in the hash passed to .to_yaml and nothing seems to be making
> any difference:
>
>
> out_file.puts newmaster.to_yaml(:SortKeys => true)
>
> newmaster is a hash and the output is definitely not being sorted.
> Unfortunately using newmaster.sort.to_yaml changes the structure of the
> yaml being output significantly (seeing as it is converted to and array
> of arrrays) and that is not desirable). Can anyone shed some light on
> this? I am using vanilla 1.8.4 install on windows and presumably the
> YAML.rb that shipped with that because I haven't installed anything
> separately.

:SortKeys isn't supported in 1.8.4. I don't know about more recent versions.

Here's the hack I use:

class Hash
def to_yaml( opts = {} )
YAML::quick_emit( object_id, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
# if opts[:SortKeys] ## fails in nesting, so let's just always sort
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
# else
# each do |k, v|
# map.add( k, v )
# end
# end
end
end
end
end


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Builder Chad

12/9/2006 3:04:00 AM

0

Joel, you are a total hero and saviour, that works like a dream. Thanks
a million!!

--
Posted via http://www.ruby-....