Documentation for Psych To_Yaml Options

Documentation for Psych to_yaml options?

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!

why does psych yaml interpreter add line breaks around 80 characters?

You'll have to configure psych’s #to_yaml options. You'll most likely find it here:

ruby-1.9.3-p125/ext/psych/emitter.c

And then you can do something like this:

yaml.to_yaml(options = {:line_width => -1})

Inconsistent wrapping of quotes in to_yaml

  • Column 1, section 2, text contains a trailing space.
  • Column 2, section 8, text contains a : character followed by a space.

The specification says:

The plain (unquoted) style has no identifying indicators and provides no form of escaping. It is therefore the most readable, most limited and most context sensitive style. In addition to a restricted character set, a plain scalar must not be empty, or contain leading or trailing white space characters. It is only possible to break a long plain line where a space character is surrounded by non-spaces.

...

Plain scalars must never contain the “: ” and “ #” character combinations. Such combinations would cause ambiguity with mapping key: value pairs and comments. In addition, inside flow collections, or when used as implicit keys, plain scalars must not contain the “[”, “]”, “{”, “}” and “,” characters. These characters would cause ambiguity with flow collection structures.

Is it possible to specify formatting options for to_yaml in ruby?

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.

Is there a way to tell Psych in ruby to using inline mode?

Psych supports this, although it isn't at all straightforward.

I've started researching this in my own question on how to dump strings using literal style.

I ended up devising a complete solution for setting various styles for specific objects, including inlining hashes and arrays.

With my script, a solution to your problem would be:

o = { 'hash' => StyledYAML.inline('name' => 'Steve', 'foo' => 'bar') }
StyledYAML.dump o, $stdout


Related Topics



Leave a reply



Submit