如何在YAML文件的顶部删除“---”?

时间:2023-01-15 09:22:51

I am modifying a YAML file in Ruby. After I write back the modified YAML, I see a --- added on top of the file. How is this getting added and how do I get rid of it?

我正在修改Ruby中的YAML文件。在我回写修改后的YAML后,我看到---添加在文件顶部。这是如何被添加的,我该如何摆脱它?

1 个解决方案

#1


32  

YAML spec says:

YAML规范说:

YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present.

YAML使用三个破折号(“---”)将指令与文档内容分开。如果没有指令,这也用于表示文档的开始。

Example:

例:

# Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey

# Team ranking
---
- Chicago Cubs
- St Louis Cardinals

So if you have multiple documents per YAML file, you have to separate them by three dashes. If you only have one document, you can remove/omit it (I never had a problem with YAML in ruby if three-dashes was missing). The reason why it's added when you yamlify your object is that, I guess, the dumper is written "by the spec" and doesn't care to implement such "shortcuts" (omit three-dashes when it's only one document).

因此,如果每个YAML文件有多个文档,则必须将它们分成三个破折号。如果您只有一个文档,则可以删除/省略它(如果缺少三个破折号,我在ruby中的YAML从未遇到过问题)。当你对对象进行yamlify时添加它的原因是,我猜,转储器是“按规范”编写的,并不关心实现这样的“快捷方式”(当它只有一个文档时省略三个破折号)。

#1


32  

YAML spec says:

YAML规范说:

YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present.

YAML使用三个破折号(“---”)将指令与文档内容分开。如果没有指令,这也用于表示文档的开始。

Example:

例:

# Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey

# Team ranking
---
- Chicago Cubs
- St Louis Cardinals

So if you have multiple documents per YAML file, you have to separate them by three dashes. If you only have one document, you can remove/omit it (I never had a problem with YAML in ruby if three-dashes was missing). The reason why it's added when you yamlify your object is that, I guess, the dumper is written "by the spec" and doesn't care to implement such "shortcuts" (omit three-dashes when it's only one document).

因此,如果每个YAML文件有多个文档,则必须将它们分成三个破折号。如果您只有一个文档,则可以删除/省略它(如果缺少三个破折号,我在ruby中的YAML从未遇到过问题)。当你对对象进行yamlify时添加它的原因是,我猜,转储器是“按规范”编写的,并不关心实现这样的“快捷方式”(当它只有一个文档时省略三个破折号)。