Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the formatting options for the to_yaml method working on ruby 1.9.1?

Tags:

ruby

yaml

to-yaml

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 ?

like image 519
jpemberthy Avatar asked Jan 28 '10 18:01

jpemberthy


1 Answers

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.

like image 176
kch Avatar answered Nov 15 '22 05:11

kch