liveBook · Manning

Escape sequence

As explained in The Well-Grounded Rubyist, Fourth Edition

Character escaping is syntax that represents special characters or prevents characters from being interpreted as delimiters or interpolation markers.

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

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through quoting, heredocs, regexp metacharacters, and dynamically constructed patterns, including the two parsing layers of Regexp.new.
Examples
Contrasts newline and tab output across quotation styles, then tests whether an interpolated dot matches a literal dot or any character.
What it assumes
Assumes familiarity with strings and method calls; introduces the relevant regexp special-character behavior through examples.
What you can do after
Apply it via Regexp.escape to match interpolated text literally, and write string escapes that survive passage to Regexp.new.
How it connects
Connects string interpolation and heredocs with regular expressions, pattern interpolation, and Regexp.escape.

Also explained in

Compare how 6 books explain escape sequence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.