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

3

16

Psych is the default yaml engine since ruby 1.9.3

Why, oh why does psych add a line break in its output? Check the example below.

ruby -v # => ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux]
require 'yaml'

"this absolutely normal sentence is more than eighty characters long because it IS".to_yaml
# => "--- this absolutely normal sentence is more than eighty characters long because it\n    IS\n...\n"

YAML::ENGINE.yamler = 'syck'

"this absolutely normal sentence is more than eighty characters long because it IS".to_yaml
# => "--- this absolutely normal sentence is more than eighty characters long because it IS\n"
Leeuwenhoek answered 25/7, 2013 at 13:46 Comment(1)
This is somewhat of a duplicate of "Documentation for Psych to_yaml options?"Mulhouse
C
19

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})
Caltanissetta answered 25/7, 2013 at 14:9 Comment(1)
Thank you :) This did the trick. Now I can continue my happy yamling.Leeuwenhoek
S
10
yaml.to_yaml(options = {:line_width => -1})

is ok to solve the problem.

but RuboCop say

Useless assignment to variable - options.

so

yaml.to_yaml(line_width: -1)

is better.

Sheathbill answered 21/10, 2015 at 10:14 Comment(0)
M
-2

Why does it matter whether YAML wraps the line or not when it serializes the data?

The question is, after wrapping it, can YAML reconstruct the correct line later when it reloads the file? And, the answer is, yes, it can:

require 'yaml'
puts '"' + YAML.load("this absolutely normal sentence is more than eighty characters long because it IS".to_yaml) + '"'

Which outputs:

"this absolutely normal sentence is more than eighty characters long because it IS"

Data that has been serialized, is in a format that YAML understands. That's an important concept, as the data is YAML's at that point. We can mess with it in an editor, and add/subtract/edit, but the data is still YAML's, because it has to reload and reparse the data in order for our applications to use it. So, after the data makes a round-trip through YAML-land, if the data returns in the same form as it left, then everything is OK.

We'd have a problem if it was serialized and then corrupted during the parsing stage, but that doesn't happen.

You can modify some of YAML's Psych driver's behavior when it's serializing data. See the answers for "Documentation for Psych to_yaml options?" for more information.

Mulhouse answered 25/7, 2013 at 14:1 Comment(2)
The purpose of using YAML is that human can edit it. Otherwise, there is not much point in using it; some binary format would be better. And since human may edit the file, it makes (some) sense to care about the appearance in YAML format.Benia
There isn't a guarantee that the data will be stored in an exact form. I know of no serializer that does that. YAML stores it in a reasonably editable/readable form, which is more readable than marshalling or JSON.Mulhouse

© 2022 - 2024 — McMap. All rights reserved.