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 reall
You can break your string
into two pieces to clarify things:
string = '\\' + '\''
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, if c
is a character that doesn't need to be escaped within a double quoted string, then \c
will be just c
. 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 == 1
The 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.
This might be worth a read : http://en.wikibooks.org/wiki/Ruby_Programming/Strings
ruby-1.9.3-p0 :002 > a = '\\\''
=> "\\'"
ruby-1.9.3-p0 :003 > a.size
=> 2
ruby-1.9.3-p0 :004 > puts a
\'
In single quotes there are only two escape characters : \\
and \'
.