为什么psych yaml解释器会在80个字符左右添加换行符?

时间:2023-01-15 00:08:09

Psych is the default yaml engine since ruby 1.9.3

自从红宝石1.9.3以来,Psych是默认的yaml引擎

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"

3 个解决方案

#1


11  

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

你必须配置psych的#to_yaml选项。你最有可能在这里找到它:

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

And then you can do something like this:

然后你可以做这样的事情:

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

#2


3  

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

is ok to solve the problem.

可以解决问题。

but RuboCop say

但是RuboCop说

Useless assignment to variable - options.

无用的赋值给变量 - 选项。

so

所以

yaml.to_yaml(line_width: -1)

is better.

更好。

#3


-1  

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

为什么YAML在序列化数据时是否包装线路是否重要?

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:

问题是,在包装之后,YAML可以在以后重新加载文件时重建正确的行吗?答案是,是的,它可以:

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.

已经序列化的数据采用YAML理解的格式。这是一个重要的概念,因为数据是YAML的。我们可以在编辑器中混淆它,并添加/减去/编辑,但数据仍然是YAML,因为它必须重新加载和重新分析数据,以便我们的应用程序使用它。因此,在数据通过YAML-land进行往返之后,如果数据以与其左侧相同的形式返回,则一切正常。

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.

您可以在序列化数据时修改YAML的一些Psych驱动程序的行为。请参阅“针对Psych to_yaml选项的文档?”的答案。了解更多信息。

#1


11  

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

你必须配置psych的#to_yaml选项。你最有可能在这里找到它:

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

And then you can do something like this:

然后你可以做这样的事情:

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

#2


3  

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

is ok to solve the problem.

可以解决问题。

but RuboCop say

但是RuboCop说

Useless assignment to variable - options.

无用的赋值给变量 - 选项。

so

所以

yaml.to_yaml(line_width: -1)

is better.

更好。

#3


-1  

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

为什么YAML在序列化数据时是否包装线路是否重要?

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:

问题是,在包装之后,YAML可以在以后重新加载文件时重建正确的行吗?答案是,是的,它可以:

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.

已经序列化的数据采用YAML理解的格式。这是一个重要的概念,因为数据是YAML的。我们可以在编辑器中混淆它,并添加/减去/编辑,但数据仍然是YAML,因为它必须重新加载和重新分析数据,以便我们的应用程序使用它。因此,在数据通过YAML-land进行往返之后,如果数据以与其左侧相同的形式返回,则一切正常。

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.

您可以在序列化数据时修改YAML的一些Psych驱动程序的行为。请参阅“针对Psych to_yaml选项的文档?”的答案。了解更多信息。