【问题标题】:How to escape quotation mark properly in this Ruby oneliner如何在这个 Ruby oneliner 中正确转义引号
【发布时间】:2014-01-18 04:36:53
【问题描述】:

我有一个像这样的 Ruby one 班轮。

ruby -e "File.open('/tmp/foo', 'a') { |f| f.puts(\"Testing \!@\#\$%\^&\*\(\)_\+\{\}\|:<>\?\") }"

在我还想在 puts() 方法中添加引号之前,一切正常。

ruby -e "File.open('/tmp/foo', 'a') { |f| f.puts(\"Testing \!@\#\$%\^&\*\(\)_\+\{\}\|:<>\?"\") }"

我不确定逃避它的正确方法是什么。有什么想法吗?

【问题讨论】:

  • 任何these ideas 帮助?
  • 我不这么认为。在我这里不是。

标签: ruby


【解决方案1】:

您可以使用 %Q{...} 或 %{} 代替 "..." 作为 ruby​​ 字符串,它们是等价的。

ruby -e "File.open('/tmp/foo', 'a') { |f| f.puts(%Q{Testing \!@\#\$%\^&\*\(\)_\+\{\}\|:<>\?\"}) }"

ruby -e "File.open('/tmp/foo', 'a') { |f| f.puts(%{Testing \!@\#\$%\^&\*\(\)_\+\{\}\|:<>\?\"}) }"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多