I’ve been working my way through the Ruby Koans and am confused by the “escape clauses and single quoted strings” examples.
One example shows that I can’t really use escape characters in this way, but immediately after, the following example is given:
def test_single_quotes_sometimes_interpret_escape_characters
string = '\\\''
assert_equal 2, string.size # <-- my answer is correct according to the program
assert_equal "\\'", string # <-- my answer is correct according to the program
end
This has confused me on two fronts:
- Single quotes can sometimes be used with escape characters.
- Why is the string size
2, whenassert_equalis"\\\'"? (I personally thought the answer was"\'", which would make more sense with regards to size).
You can break your
stringinto two pieces to clarify things:Each part is a string of length one;
'\\'is the single character\and'\''is the single character'. When you put them together you get the two character string\'.There are two characters that are special within a single quoted string literal: the backslash and the single quote itself. The single quote character is, of course, used to delimit the string so you need something special to get a single quote into a single quoted string, the something special is the backslash so
'\''is a single quoted string literal that represents a string containing one single quote character. Similarly, if you need to get a backslash into a single quoted string literal you escape it with another backslash so'\\'has length one and contains one backslash.The single quote character has no special meaning within a double quoted string literal so you can say
"'"without any difficulty. The backslash, however, does have a special meaning in double quoted strings so you have to say"\\"to get a single backslash into your double quoted string.Consider your guess off
"\'". The single quote has no special meaning within a double quoted string and escaping something that doesn’t need escaping just gives you your something back; so, ifcis a character that doesn’t need to be escaped within a double quoted string, then\cwill be justc. In particular,"\'"evaluates to"'"(i.e. one single quote within a double quoted string).The result is that:
'\\\'' == "\\'""\\\"" == '\\"'"\'" == '\''"\'" == "'"'\\\''.length == 2"\\\"".length == 2"\'".length == 1"'".length == 1The Wikibooks reference that Kassym gave covers these things.
I usually switch to
%q{}(similar to single quoting) or%Q{}(similar to double quoting) when I need to get quotes into strings, all the backslashes make my eyes bleed.