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

发布于 2024-08-19 08:16:09 字数 1244 浏览 6 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-08-26 08:16:09

在不久的将来,我已经为此深入研究了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文