Documentation for Psych to_yaml options?

旧街凉风 提交于 2019-11-29 16:08:55

问题


Ruby 1.9.3 defaults to using Psych for YAML. While the ruby-doc documentation for it is completely lacking, I was able to find one external piece of documentation that hinted that the indentation option is supported. This was borne out in testing:

irb(main):001:0> RUBY_VERSION
#=> "1.9.3"
irb(main):002:0> require 'yaml'
#=> true
irb(main):003:0> [[[1]]].to_yaml
#=> "---\n- - - 1\n"
irb(main):009:0> [[[1]]].to_yaml indentation:9
#=> "---\n-        -        - 1\n"

There are presumably more options supported. Specifically, I want to know how to change the line wrap width or disable it altogether.

What are the options available?


回答1:


Deep in the guts of ruby-1.9.3-p125/ext/psych/emitter.c I found three options:

  • indentation - The level must be less than 10 and greater than 1.
  • line_width - Set the preferred line width.
  • canonical - Set the output style to canonical, or not (true/false).

And they work!




回答2:


When you want to disable line wrap, use this option:

line_width: -1


来源:https://stackoverflow.com/questions/9759302/documentation-for-psych-to-yaml-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!