Documentation for Psych to_yaml options?
Asked Answered
H

2

28

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?

Hierarch answered 18/3, 2012 at 14:53 Comment(1)
After looking at the source of 2.2.0, a short update: It seems there are very few options right now, hence little documentation still.Mlle
H
43

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!

Hierarch answered 18/3, 2012 at 14:59 Comment(4)
So, it seems you can't disable line wrap? I tried line_width: 0 and with nil which do not have any effect. true returns an error.Menstruate
@Menstruate You want line_width:999 or some other sufficiently large number.Hierarch
It seems Syck options doesn't work too. So these ones for Psych are only avalible for 1.9.3Zaria
I had line_width to 999, which still had some weird newlines. -1 worked, thanks @MenstruateLippold
M
19

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

line_width: -1
Menstruate answered 27/9, 2012 at 3:22 Comment(1)
Neither foo.to_yaml(line_width: -1) or foo.to_yaml(options = {line_width: -1}) is working for me in ruby 2.5.xClevey

© 2022 - 2024 — McMap. All rights reserved.