如何获取在 ruby 1.9.1 上运行的 to_yaml 方法的格式选项?
根据 YAML 文档,可以将 选项哈希 传递给 .to_yaml
方法。
目前,当我按照文档建议传递选项时,它不起作用,哈希将被忽略。
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"
现在,传递一些选项:
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"
正如您所看到的,传递选项不起作用。仅默认值:
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}
这是一个已知的错误吗?或者它目前适用于任何使用 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:
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不久的将来,我已经为此深入研究了 C 源代码。我发帖只是为了验证评论中已经说过的内容。
基本上是做不到的。在到达 YAML 编写器之前,Syck 选项会在该过程中的某个地方丢失。
最好的就是
to_yaml_style
。有时。1.8 和 1.9 也是如此。
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.