YAML:我需要为 YAML 中的字符串加上引号吗? [英] YAML: Do I need quotes for strings in YAML?

查看:53
本文介绍了YAML:我需要为 YAML 中的字符串加上引号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Rails 项目的国际化编写 YAML 字典.不过,我有点困惑,因为在某些文件中我看到双引号中的字符串,而在某些文件中则没有.需要考虑的几点:

I am trying to write a YAML dictionary for internationalisation of a Rails project. I am a little confused though, as in some files I see strings in double-quotes and in some without. A few points to consider:

  • 示例 1 - 所有字符串都使用双引号;
  • 示例 2 - 无字符串 (除了最后两个)使用引号;
  • YAML 食谱 说:用双引号括起来的字符串允许您可以使用转义来表示 ASCII 和 Unicode 字符. 这是否意味着只有在我想转义某些字符时才需要使用双引号?如果是的话——为什么他们在第一个例子中到处都使用双引号——只是为了统一/风格的原因?
  • 示例 2 的最后两行使用 ! - 非特定标记,而第一个示例的最后两行不使用 - 它们都有效.
  • example 1 - all strings use double quotes;
  • example 2 - no strings (except the last two) use quotes;
  • the YAML cookbook says: Enclosing strings in double quotes allows you to use escaping to represent ASCII and Unicode characters. Does this mean I need to use double quotes only when I want to escape some characters? If yes - why do they use double quotes everywhere in the first example - only for the sake of unity / stylistic reasons?
  • the last two lines of example 2 use ! - the non-specific tag, while the last two lines of the first example don't - and they both work.

我的问题是:在 YAML 中使用不同类型的引号的规则是什么?

My question is: what are the rules for using the different types of quotes in YAML?

可以这么说吗:

  • 一般来说,您不需要引号;
  • 如果要转义字符,请使用双引号;
  • 使用带有单引号的 !,当... ?!?
  • in general, you don't need quotes;
  • if you want to escape characters use double quotes;
  • use ! with single quotes, when... ?!?

推荐答案

在简要回顾了问题中引用的 YAML 食谱并进行了一些测试后,这是我的解释:

After a brief review of the YAML cookbook cited in the question and some testing, here's my interpretation:

  • 一般来说,您不需要引号.
  • 使用引号强制字符串,例如如果您的键或值是 10 但您希望它返回 String 而不是 Fixnum,请编写 '10'"10".
  • 如果您的值包含特殊字符,请使用引号,(例如 :{}[], ,, &, *, #, ?, |, -, <, >, =, !, %, @, ).
  • 单引号可以让您在字符串中放置几乎任何字符,并且不会尝试解析转义码.' ' 将作为字符串 返回.
  • 双引号解析转义码." " 将作为换行符返回.
  • 感叹号介绍了一种方法,例如!ruby/sym 返回一个 Ruby 符号.
  • In general, you don't need quotes.
  • Use quotes to force a string, e.g. if your key or value is 10 but you want it to return a String and not a Fixnum, write '10' or "10".
  • Use quotes if your value includes special characters, (e.g. :, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, @, ).
  • Single quotes let you put almost any character in your string, and won't try to parse escape codes. ' ' would be returned as the string .
  • Double quotes parse escape codes. " " would be returned as a line feed character.
  • The exclamation mark introduces a method, e.g. !ruby/sym to return a Ruby symbol.

在我看来,最好的方法是不使用引号,除非你必须这样做,然后使用单引号,除非你特别想处理转义码.

Seems to me that the best approach would be to not use quotes unless you have to, and then to use single quotes unless you specifically want to process escape codes.

更新

是"和否"应该用引号括起来(单引号或双引号),否则它们将被解释为 TrueClass 和 FalseClass 值:

"Yes" and "No" should be enclosed in quotes (single or double) or else they will be interpreted as TrueClass and FalseClass values:

en:
  yesno:
    'yes': 'Yes'
    'no': 'No'

这篇关于YAML:我需要为 YAML 中的字符串加上引号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆