# YAML extension not honouring the options passed to .to\_yaml

**URL:** <https://www.ruby-forum.com/t/yaml-extension-not-honouring-the-options-passed-to-to-yaml/78845>\
**Category:** Ruby\
**Created:** [December 9, 2006, 3:40am UTC](https://www.ruby-forum.com/t/yaml-extension-not-honouring-the-options-passed-to-to-yaml/78845 "2006-12-09T03:40:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 3:40am UTC](https://www.ruby-forum.com/t/yaml-extension-not-honouring-the-options-passed-to-to-yaml/78845/1 "2006-12-09T03:40:11Z")

</div>

As outlined on [http://yaml4r.sourceforge.net/doc/](http://yaml4r.sourceforge.net/doc/) 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.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 3:58am UTC](https://www.ruby-forum.com/t/yaml-extension-not-honouring-the-options-passed-to-to-yaml/78845/2 "2006-12-09T03:58:48Z")

</div>

Chad T. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 4:04am UTC](https://www.ruby-forum.com/t/yaml-extension-not-honouring-the-options-passed-to-to-yaml/78845/3 "2006-12-09T04:04:09Z")

</div>

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