Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can break your <code>string</code> into two pieces to clarify things:</p> <pre><code>string = '\\' + '\'' </code></pre> <p>Each part is a string of length one; <code>'\\'</code> is the single character <code>\</code> and <code>'\''</code> is the single character <code>'</code>. When you put them together you get the two character string <code>\'</code>.</p> <p>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 <em>something special</em> is the backslash so <code>'\''</code> 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 <code>'\\'</code> has length one and contains one backslash.</p> <p>The single quote character has no special meaning within a double quoted string literal so you can say <code>"'"</code> without any difficulty. The backslash, however, does have a special meaning in double quoted strings so you have to say <code>"\\"</code> to get a single backslash into your double quoted string.</p> <p>Consider your guess off <code>"\'"</code>. The single quote has no special meaning within a double quoted string and escaping something that doesn't need escaping just gives you your <em>something</em> back; so, if <code>c</code> is a character that doesn't need to be escaped within a double quoted string, then <code>\c</code> will be just <code>c</code>. In particular, <code>"\'"</code> evaluates to <code>"'"</code> (i.e. one single quote within a double quoted string).</p> <p>The result is that:</p> <ul> <li><code>'\\\'' == "\\'"</code></li> <li><code>"\\\"" == '\\"'</code></li> <li><code>"\'" == '\''</code></li> <li><code>"\'" == "'"</code></li> <li><code>'\\\''.length == 2</code></li> <li><code>"\\\"".length == 2</code></li> <li><code>"\'".length == 1</code></li> <li><code>"'".length == 1</code></li> </ul> <p>The <a href="http://en.wikibooks.org/wiki/Ruby_Programming/Strings">Wikibooks reference</a> that Kassym gave covers these things.</p> <p>I usually switch to <a href="http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation"><code>%q{}</code> (similar to single quoting) or <code>%Q{}</code> (similar to double quoting)</a> when I need to get quotes into strings, all the backslashes make my eyes bleed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload