tags:

views:

209

answers:

1

According to the YAML documentation it's possible to pass a hash of options to the .to_yaml method.

Currently when I pass the options as suggested by the documentation it's not working, the hash is being ignored.

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> user = { "1" => { "name" => "john", "age" => 44 } }
user.to_yaml
=> "--- \n\"1\": \n  name: john\n  age: 44\n"

Now, passing some options:

irb(main):014:0> user.to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true )
=> "--- \n\"1\": \n  name: john\n  age: 44\n"
irb(main):015:0> user.to_yaml( :Separator => "\n" )
=> "--- \n\"1\": \n  name: john\n  age: 44\n"
irb(main):016:0> user.to_yaml( :separator => "\n" )
=> "--- \n\"1\": \n  name: john\n  age: 44\n"
irb(main):017:0> RUBY_VERSION
=> "1.9.1"

As you can see, passing the options don't work. Only the defaults:

YAML::DEFAULTS
=> {:Indent=>2, :UseHeader=>false, :UseVersion=>false, :Version=>"1.0", :SortKeys=>false, :AnchorFormat=>"id%03d", :ExplicitTypes=>false, :WidthType=>"absolute", :BestWidth=>80, :UseBlock=>false, :UseFold=>false, :Encoding=>:None}

Is this a known bug? or It's currently working for anyone using Ruby 1.9.1 ?

+1  A: 

I have dug relatively deep into the C source for this in the not so distant past. I'm posting just to validate what's already been said in the comments.

Basically, can't do it. The Syck options get lost somewhere in the process, before ever hitting the YAML writer.

The best you can have is to_yaml_style. Sometimes.

This is the same for 1.8 and 1.9.

kch
Ok, well I ended writing a small module to get it formatted, not as optimal as it should be using the `to_yaml` method ... but at least it works. Thanks.
jpemberthy