Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 524
mla Avatar asked Jul 25 '13 13:07

mla


2 Answers

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})
like image 125
Ryan Rich Avatar answered Oct 05 '22 09:10

Ryan Rich


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.

like image 24
shingo.nakanishi Avatar answered Oct 05 '22 11:10

shingo.nakanishi