Ruby 的 %q / %Q 引用方法的用例是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10144543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:00:29  来源:igfitidea点击:

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

rubystringquotes

提问by Michael Berkowski

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.

我一直在阅读 Thomas 的 Programming Ruby 1.9,并发现了替代的分隔单引号和双引号方法 ( %q / %Q)。我也从其他 Ruby 语言参考资料中了解到它们。

%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 neverencountered this quoting method in production code.

我使用 Ruby 的时间不长,但我从未在生产代码中遇到过这种引用方法。

Other than the obvious ability to avoid internally escaping quoteswith 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?

除了明显能够避免使用反斜杠在内部转义引号之外,这种引用常规单引号或双引号的方法的常见用例是什么?它们通常用于单行还是多行字符串?如果在多行字符串中使用,它们是否比 HEREDOC 字符串更受青睐?是否有特定的 Ruby 习语在它们常见的地方?

回答by tadman

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

它们对于使用 JavaScript 转义 HTML 非常有用,其中您已经“用完了”引用方法:

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:

我还发现它们在处理多行 SQL 语句时非常有用:

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.

这样做的好处是您无需注意查询中使用的引用类型。它适用于单人、双人或两者。它们也比 HEREDOC 风格的方法少很多麻烦。

Ruby provides other convenience methods like this such as %rwhich 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.

Ruby 提供了其他类似的便捷方法,例如%r可以构造正则表达式。当试图编写一个处理类似的东西时,这避免了斜线炎http://,否则就必须转义。

回答by dreftymac

Overview

概述

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

除了“避免内部转义引号”和@tadman 之前提供的示例之外,还有其他用例:

  • 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)
  • 使用与生成器本身相同的语言自动生成代码(例如,Ruby 生成 Ruby)
  • 提供格式清晰的代码,不会混淆文本编辑器的语法突出显示功能
  • 允许存储可能必须通过多个存储层的代码块(例如与网站交互的数据库,或与文本编辑器交互的代码段管理系统,与子外壳交互等)

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.

这种方法是一种通用且健壮的习惯用法,适用于任何类型的自动代码生成工具,包括但不限于用其他语言编写样板代码的工具,或管理 IDE 或文本编辑器代码片段的工具.

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.

除了@tadman 已经提供的示例之外,还有生成代码的一般情况,其中生成的代码与生成程序的代码具有相同或基本相似的语法。

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.,

[参见例如,

]

]

回答by Dan Garland

Its perhaps worth noting that %q is used by bundler and jeweler by default when generating gemspecs, for the summary/description section of the gemspec. This prevents someone from using quotes in the summary or description and breaking the gemspec.

值得一提的是,在生成 gemspec 时,捆绑器和珠宝商默认使用 %q,用于 gemspec 的摘要/描述部分。这可以防止有人在摘要或描述中使用引号并破坏 gemspec。

回答by Ryan Rebo

I know this is an old thread, but I have used them in production when calling a systemcommand and wanting to interpolate some values, like so:

我知道这是一个旧线程,但是在调用系统命令并想要插入一些值时,我在生产中使用了它们,如下所示:

 system(%Q(ffmpeg -y -i "#{input_filepath}" -qscale:a 2 "#{output_filepath}"))

Super handy.

超级好用。

回答by Peter

They are useful when your string contains single- or double- quotes. This doesn't happen all that often, but the technique is very useful when it does.

当您的字符串包含单引号或双引号时,它们很有用。这种情况并不经常发生,但该技术在发生时非常有用。