I\'ve been reading through Thomas\' Programming Ruby 1.9 and came upon the alternative delimited single and double-quoting methods (%q / %Q). I\'ve known of the
Apart from "avoid internally escaping quotes" and the examples previously provided by @tadman there are other use-cases as well:
This approach is a general-purpose and robust idiom that works well with any kind of tool that does automated code generation, including but not limited to tools that write boilerplate code in other languages, or tools that manage code snippets for an IDE or text editor.
Besides the examples already provided by @tadman, there is the general case of generating code where the code being generated is the same or substantially similar syntax as the code of the generating program.
In these cases, the solution does a lot more than help with avoiding the use of backslashes to escape quotes. Without a solution like this, there are cases where the generated code can get extremely difficult to maintain.
To see examples of this, feel free to take a look at the following references.
[ See e.g.,
]