我需要在YAML中爲字符串加引號嗎?

本文翻譯自:Do I need quotes for strings in YAML?

I am trying to write a YAML dictionary for internationalization of a Rails project. 我正在嘗試編寫有關Rails項目國際化的YAML字典。 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: 需要考慮的幾點:

  • example 1 - all strings use double quotes; 示例1-所有字符串都使用雙引號;
  • example 2 - no strings (except the last two) use quotes; 示例2-沒有字符串(最後兩個除外)使用引號;
  • the YAML cookbook says: Enclosing strings in double quotes allows you to use escapings to represent ASCII and Unicode characters. YAML食譜說: 用雙引號引起來的字符串使您可以使用轉義符來表示ASCII和Unicode字符。 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 ! 示例2的最後兩行使用! - the non-specific tag, while the last two lines of the first example don't - and they both work. -非特定標記,而第一個示例的最後兩行則無效-它們都起作用。

My question is: what are the rules for using the different types of quotes in YAML? 我的問題是:在YAML中使用不同類型的報價的規則是什麼?

Could it be said that: 可以這樣說:

  • in general, you don't need quotes; 通常,您不需要引號;
  • if you want to escape characters use double quotes; 如果要轉義字符,請使用雙引號;
  • use ! 使用! with single quotes, when... ?!? 用單引號括起來,何時...?!?

#1樓

參考:https://stackoom.com/question/1IBMO/我需要在YAML中爲字符串加引號嗎


#2樓

After a brief review of the YAML cookbook cited in the question and some testing, here's my interpretation: 在對問題中引用的YAML食譜進行簡短回顧並進行了一些測試之後,以下是我的解釋:

  • In general, you don't need quotes. 通常,您不需要引號。
  • Use quotes to force a string, eg if your key or value is 10 but you want it to return a String and not a Fixnum, write '10' or "10" . 使用引號強制輸入字符串,例如,如果您的鍵或值是10但您希望它返回字符串而不是Fixnum,則輸入'10'"10"
  • Use quotes if your value includes special characters, (eg : , { , } , [ , ] , , , & , * , # , ? , | , - , < , > , = , ! , % , @ , \\ ). 使用引號如果值包含特殊字符(例如:{}[],&*#?|- <>=!%@\\ )。
  • Single quotes let you put almost any character in your string, and won't try to parse escape codes. 單引號使您幾乎可以在字符串中放置任何字符,並且不會嘗試解析轉義碼。 '\\n' would be returned as the string \\n . '\\n'將作爲字符串\\n返回。
  • Double quotes parse escape codes. 雙引號解析轉義碼。 "\\n" would be returned as a line feed character. "\\n"將作爲換行符返回。
  • The exclamation mark introduces a method, eg !ruby/sym to return a Ruby symbol. 感嘆號引入了一種方法,例如!ruby/sym以返回Ruby符號。

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. 在我看來,最好的方法是除非必須使用引號,否則不要使用引號,除非您特別想處理轉義碼,否則使用單引號。

Update 更新資料

"Yes" and "No" should be enclosed in quotes (single or double) or else they will be interpreted as TrueClass and FalseClass values: “是”和“否”應用引號引起來(單引號或雙引號),否則它們將被解釋爲TrueClass和FalseClass值:

en:
  yesno:
    'yes': 'Yes'
    'no': 'No'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章