Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case for Ruby's %q / %Q quoting methods?

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 them from other Ruby language references as well.

%q/I'm acting like a single-quoted string/  %Q|"I'm acting like a double-quoted string" --Anonymous| 

I have not been working with Ruby for long, but I have never encountered this quoting method in production code.

Other than the obvious ability to avoid internally escaping quotes with backslashes, what are the common use cases for this method of quoting over regular single or double quotes? Are they typically used in single or multiline strings? If used in multiline strings, are they ever favored over HEREDOC strings? Is there a particular Ruby idiom where they're commonly found?

like image 880
Michael Berkowski Avatar asked Apr 13 '12 16:04

Michael Berkowski


People also ask

What is Ruby quote?

Ruby | Regexp quote() function. Regexp#quote() : quote() is a Regexp class method which escapes any characters that would have special meaning in a regular expression. Return: escapes any characters that would have special meaning in a regular expression.

Can you use single quotes in Ruby?

If you've written Ruby, you've heard it before: Use single quoted strings unless you need string interpolation.

How do you escape a double quote in Ruby?

Escaping quotes When using strings in Ruby, we sometimes need to put the quote we used to define the string inside the string itself. When we do, we can escape the quote character with a backslash \ symbol.


2 Answers

They're extraordinarily useful for escaping HTML with JavaScript in it where you've already "run out" of quoting methods:

link = %q[<a href="javascript:method('call')">link</a>] 

I've also found them to be very useful when working with multi-line SQL statements:

execute(%Q[   INSERT INTO table_a (column_a)     SELECT value       FROM table_b       WHERE key='value' ]) 

The advantage there is you don't need to pay attention to the type of quoting used within your query. It will work with either single, double, or both. They're also a lot less fuss than the HEREDOC style method.

Ruby provides other convenience methods like this such as %r which can construct regular expressions. That avoids slash-itis when trying to write one that handles stuff like http:// that would otherwise have to be escaped.

like image 143
tadman Avatar answered Sep 25 '22 01:09

tadman


Overview

Apart from "avoid internally escaping quotes" and the examples previously provided by @tadman there are other use-cases as well:

  • auto-generating code in the same language as the generator itself (e.g., Ruby generating Ruby)
  • providing cleanly-formatted code that does not confuse the syntax-highlighting feature of your text editor
  • allow storage of codeblocks that may have to pass through multiple storage layers (such as a database interacting with a website, or a snippets management system interacting with a text editor, which interacts with a subshell, and so forth)

Details

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.

References

[ See e.g.,

  • http://en.wikipedia.org/wiki/Leaning_toothpick_syndrome
  • http://c2.com/cgi/wiki?QuineProgram
  • http://en.wikipedia.org/wiki/Delimiter#Delimiter_collision
  • http://en.wikipedia.org/wiki/Quine_(computing)

]

like image 44
dreftymac Avatar answered Sep 27 '22 01:09

dreftymac