YAML: Do I need quotes for strings in YAML?
While Mark's answer nicely summarizes when the quotes are needed according to the YAML language rules, I think what many of the developers/administrators are asking themselves, when working with strings in YAML, is "what should be my rule of thumb for handling the strings?"
It may sound subjective, but the number of rules you have to remember, if you want to use the quotes only when they are really needed as per the language spec, is somewhat excessive for such a simple thing as specifying one of the most common datatypes. Don't get me wrong, you will eventually remember them when working with YAML regularly, but what if you use it occasionally, and you didn't develop automatism for writing YAML? Do you really want to spend time remembering all the rules just to specify the string correctly?
The whole point of the "rule of thumb" is to save the cognitive resource and to handle a common task without thinking about it. Our "CPU" time can arguably be used for something more useful than handling the strings correctly.
From this - pure practical - perspective, I think the best rule of thumb is to single quote the strings. The rationale behind it:
- Single quoted strings work for all scenarios, except when you need to use escape sequences.
- The only special character you have to handle within a single-quoted string is the single quote itself.
These are just 2 rules to remember for some occasional YAML user, minimizing the cognitive effort.
After a brief review of the YAML cookbook cited in the question and some testing, here's my interpretation:
- 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.
'\n'
would be returned as the string\n
. - Double quotes parse escape codes.
"\n"
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.
Update
"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'