开发者

How to get the formatting options for the to_yaml method working on ruby 1.9.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:

YA开发者_如何学运维ML::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 ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜