是否可以在 ruby​​ 中指定 to_yaml 的格式选项?

发布于 2024-07-25 20:11:10 字数 778 浏览 8 评论 0原文

代码

require 'yaml'
puts YAML.load("
is_something:
  values: ['yes', 'no']
").to_yaml

生成

--- 
is_something: 
  values: 
  - "yes"
  - "no"

虽然这是一个正确的 yaml,但当您有数组哈希时,它看起来很丑陋。 有没有办法让我生成 yaml 的内联数组版本?

选项哈希可以传递到 < code>to_yaml 但你如何使用它?

编辑 0:谢谢 Pozsár Balázs。 但是,从 ruby​​ 1.8.7 (2009-04-08 patchlevel 160) 开始,选项哈希并不像宣传的那样工作。 :(

irb
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> puts [[ 'Crispin', 'Glover' ]].to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true )
--- 
- - Crispin
  - Glover
=> nil

The code

require 'yaml'
puts YAML.load("
is_something:
  values: ['yes', 'no']
").to_yaml

produces

--- 
is_something: 
  values: 
  - "yes"
  - "no"

While this is a correct yaml, it just looks ugly when you have a hash of arrays. Is there a way for me to get to_yaml to produce the inline array version of the yaml?

An options hash can be passed to to_yaml but how do you use it?

Edit 0: Thanks Pozsár Balázs. But, as of ruby 1.8.7 (2009-04-08 patchlevel 160), the options hash does not work as advertised. :(

irb
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> puts [[ 'Crispin', 'Glover' ]].to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true )
--- 
- - Crispin
  - Glover
=> nil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

月亮邮递员 2024-08-01 20:11:10

关于哈希选项:请参阅 http://yaml4r.sourceforge.net/doc/page/examples .htm

例如。 24:使用带有选项哈希

puts [[ 'Crispin', 'Glover' ]].to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true )
# prints:
#   --- %YAML:1.0
#   -
#       - Crispin
#       - Glover

Ex的to_yaml。 25:选项哈希的可用符号

Indent:发出时使用的默认缩进(默认为2
Separator:文档之间使用的默认分隔符(默认为'---'
SortKeys:发出时对哈希键进行排序? (默认为 false
UseHeader:发出时显示 YAML 标头? (默认为 false
UseVersion:发出时显示YAML版本? (默认为 false
AnchorFormat:发送时锚点 ID 的格式化字符串(默认为“id%03d”)
ExplicitTypes:发出时使用显式类型? (默认为 false
BestWidth:折叠文本时使用的字符宽度(默认为 80
UseFold:发射时强制折叠文本? (默认为 false
UseBlock:在发出时强制所有文本均为文字? (默认为 false
编码:编码时使用的 Unicode 格式(默认为 :Utf8;需要 Iconv)

About the hash options: see http://yaml4r.sourceforge.net/doc/page/examples.htm

Ex. 24: Using to_yaml with an options Hash

puts [[ 'Crispin', 'Glover' ]].to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true )
# prints:
#   --- %YAML:1.0
#   -
#       - Crispin
#       - Glover

Ex. 25: Available symbols for an options Hash

Indent: The default indentation to use when emitting (defaults to 2)
Separator: The default separator to use between documents (defaults to '---')
SortKeys: Sort Hash keys when emitting? (defaults to false)
UseHeader: Display the YAML header when emitting? (defaults to false)
UseVersion: Display the YAML version when emitting? (defaults to false)
AnchorFormat: A formatting string for anchor IDs when emitting (defaults to 'id%03d')
ExplicitTypes: Use explicit types when emitting? (defaults to false)
BestWidth: The character width to use when folding text (defaults to 80)
UseFold: Force folding of text when emitting? (defaults to false)
UseBlock: Force all text to be literal when emitting? (defaults to false)
Encoding: Unicode format to encode with (defaults to :Utf8; requires Iconv)

何以畏孤独 2024-08-01 20:11:10

从 Ruby 1.9 开始,使用 psych 作为默认的 YAML 引擎。 它支持一些属性: http:// ruby-doc.org/stdlib-2.1.0/libdoc/psych/rdoc/Psych/Handler/DumperOptions.html

所以对我来说它有效:

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> puts [{'a'=> 'b', 'c'=> 'd'}, {'e'=> 'f', 'g'=>'h'}].to_yaml(:indentation => 4)
---
-   a: b
    c: d
-   e: f
    g: h

Starting from Ruby 1.9 psych is used as a default YAML engine. It supports some attributes: http://ruby-doc.org/stdlib-2.1.0/libdoc/psych/rdoc/Psych/Handler/DumperOptions.html

So for me it works:

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> puts [{'a'=> 'b', 'c'=> 'd'}, {'e'=> 'f', 'g'=>'h'}].to_yaml(:indentation => 4)
---
-   a: b
    c: d
-   e: f
    g: h
早乙女 2024-08-01 20:11:10

这个丑陋的黑客似乎可以解决问题......

class Array
  def to_yaml_style
    :inline
  end
end

浏览 ruby​​ 的源代码,我找不到任何可以通过的选项来实现相同的目的。 默认选项在 lib/yaml/constants.rb 中描述

This ugly hack seems to do the trick...

class Array
  def to_yaml_style
    :inline
  end
end

Browsing through ruby's source, I can't find any options I could pass to achieve the same. Default options are described in the lib/yaml/constants.rb.

寄人书 2024-08-01 20:11:10

只是指定输出样式的另一种技巧,但是这个允许针对每个特定对象自定义它,而不是全局(例如对于所有数组)。

简单的例子

class Movie
  attr_accessor :genres, :actors

  # method called by psych to render YAML
  def encode_with(coder)
    # render array inline (flow style)
    coder['genres'] = StyledYAML.inline(genres) if genres
    # render in default style (block)
    coder['actors'] = actors if actors
  end
end

Just another hack to specify the output style, but this one allows to customize it per specific object, instead of globally (e.g. for all arrays).

https://gist.github.com/jirutka/31b1a61162e41d5064fc

Simple example:

class Movie
  attr_accessor :genres, :actors

  # method called by psych to render YAML
  def encode_with(coder)
    # render array inline (flow style)
    coder['genres'] = StyledYAML.inline(genres) if genres
    # render in default style (block)
    coder['actors'] = actors if actors
  end
end
唐婉 2024-08-01 20:11:10

最新版本的 Ruby 使用 Psych 模块进行 YAML 解析。 您可以传递的选项并不多,但您可以更改缩进和线宽。 查看最新的 Psych 文档< /a> 了解更多详细信息。

The latest versions of Ruby use the Psych module for YAML parsing. There aren't many options that you can pass but you can change indention and line width. Check the latest Psych documentation for more details.

自找没趣 2024-08-01 20:11:10

直接使用Psych。

缩进没有效果:
my_yaml.to_yaml(:indentation => 2)

缩进有效:
Psych.dump(my_yaml, :indentation => 8)

Use Psych directly.

Indentation has no effect:
my_yaml.to_yaml(:indentation => 2)

Indentation works:
Psych.dump(my_yaml, :indentation => 8)

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