ruby YAML 每个缩进多少个空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42247535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:52:25  来源:igfitidea点击:

YAML How many spaces per indent?

rubyyamlamazon-elastic-beanstalk

提问by Anestis Kivranoglou

Is there any difference if i use one space, two or four spaces per indent level in YAML?

如果我在YAML 中的每个缩进级别使用一个空格、两个或四个空格有什么区别吗?

Are there any specific rules for space numbers per Structure type??

每种结构类型的空间编号是否有任何特定规则?

For example 4spaces for nesting maps, 1space per list itemetc??

例如用于嵌套地图的4 个空格,每个列表项1 个空格等??

I am writing a yaml configuration file for elastic beanstalk .ebextensions and i am having really hard time constructing this correctly. Although i have valid yaml in YAML Validatorelastic beanstalk seems to understand a different structure.

我正在为弹性 beanstalk .ebextensions 编写一个 yaml 配置文件,我很难正确构建它。虽然我在YAML 验证器中有有效的 yaml,但弹性 beantalk 似乎理解不同的结构。

回答by flyx

There is no requirement in YAML to indent any concrete number of spaces. There is also no requirement to be consistent. So for example, this is valid YAML:

YAML 中不要求缩进任何具体数量的空格。也没有要求保持一致。例如,这是有效的 YAML:

a:
 b:
     - c
     -  d
     - e
f:
    "ghi"

Some rules might be of interest:

一些规则可能很有趣:

  • Flow content (i.e. everything that starts with {or [) can span multiple lines, but must be indented at least as many spaces as the surrounding current block level.
  • Block list items can(but don't need to) have the same indentation as the surrounding block level because -is considered part of the indentation:
  • 流内容(即以{或开头的所有内容[)可以跨越多行,但必须缩进至少与周围当前块级别一样多的空格。
  • 块列表项可以(但不需要)与周围的块级别具有相同的缩进,因为-被认为是缩进的一部分:
a:    # top-level key
- b   # value of that key, which is a list
- c
c:    # next top-level key
 d    # non-list value which must be more indented

回答by sevenr

The YAML spec for v 1.2merely says that

v 1.2YAML 规范只是说

In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line.

To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently. Note that most modern editors may be configured so that pressing the tab key results in the insertion of an appropriate number of spaces.

The amount of indentation is a presentation detail and must not be used to convey content information.

在 YAML 块样式中,结构由缩进决定。通常,缩进被定义为行首的零个或多个空格字符。

为了保持可移植性,不能在缩进中使用制表符,因为不同的系统对制表符的处理方式不同。请注意,大多数现代编辑器可能被配置为按下 Tab 键会导致插入适当数量的空格。

缩进的数量是表示细节,不得用于传达内容信息。

So you can set the indent depth to your preference, as long as you use spaces and not tabs. Interestingly, IntelliJ uses 2 spaces by default.

因此,只要您使用空格而不是制表符,您就可以根据自己的喜好设置缩进深度。有趣的是,IntelliJ 默认使用 2 个空格。