简体   繁体   English

如何获取在ruby 1.9.1上运行的to_yaml方法的格式化选项?

[英]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. 根据YAML文档,可以将选项散列传递给.to_yaml方法。

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 ? 或者它目前适用于使用Ruby 1.9.1的任何人?

I have dug relatively deep into the C source for this in the not so distant past. 在不太遥远的过去,我已经深入挖掘了C源。 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. 在击中YAML编写器之前,Syck选项在进程的某个地方丢失了。

The best you can have is to_yaml_style . 你可以拥有的最好的是to_yaml_style Sometimes. 有时。

This is the same for 1.8 and 1.9. 对于1.8和1.9,这是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM