A backslash changes what Ruby reads
In a double-quoted string, `"Line one\nLine two"` contains a newline, while `"\#{2 + 2}"` contains the literal characters `#{2 + 2}` instead of interpolating the expression. The backslash can request a special character or prevent syntax from taking effect. section 8.1.1
Quotation style matters. Double-quoted strings interpret `\n` as a newline and `\t` as a tab; single-quoted strings leave those sequences as visible text. Escape a string’s own quotation mark to include it without ending the string. In double quotes, write `\\` when you need a literal backslash. section 8.1.1
The same distinction applies to heredocs: ordinary heredocs behave like double-quoted strings, including interpolation and character escapes. Quote the opening delimiter as in `<<-'EOM'` to keep `\n` and `#{2+2}` literal. Alternative `%q` notation can also avoid escaping apostrophes and quotation marks when its chosen delimiters do not conflict with the content. section 8.1.1
A regular expression gives escaping another job. Characters such as `?` and `.` have pattern meanings; `/\?/` matches a question mark itself. Delimiters matter too: a slash must be escaped inside `/.../`, but not inside `%r{...}`. section 11.3.1
Escape text before making it a pattern
With `str = "a.c"`, `/#{str}/` matches both `"a.c"` and `"abc"`: the inserted dot becomes a wildcard. Use `/#{Regexp.escape(str)}/` to match the dot literally, so `"abc"` no longer matches. `Regexp.escape` handles the pattern escaping when the text is supplied dynamically. section 11.6.1
When passing a string to `Regexp.new`, check both layers: Ruby first reads the string, then the regexp parser reads its contents. A single-quoted string can pass `'Mr\.'` through as a backslash and dot; a double-quoted string needs `"Mr\\."` to pass the same characters. Otherwise, the dot reaches the pattern unescaped and acts as a wildcard. section 11.6.1





