【问题标题】:Clean way to build long strings in Ruby在 Ruby 中构建长字符串的简洁方法
【发布时间】:2023-03-21 21:17:01
【问题描述】:

在编写 Ruby(客户端脚本)时,我看到了三种构建更长字符串的方法,包括行尾,所有这些对我来说都“闻起来”有点难看。

有没有更干净更好的方法?

变量递增。

if render_quote?
  quote =  "Now that there is the Tec-9, a crappy spray gun from South Miami."
  quote += "This gun is advertised as the most popular gun in American crime. Do you believe that shit?"
  quote += "It actually says that in the little book that comes with it: the most popular gun in American crime."
  quote += "Like they're actually proud of that shit."
  puts quote
end

Heredocs(和未闭合的引号)。

if render_quote?
  quote =<<EOS
Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime. Do you believe that shit?
It actually says that in the little book that comes with it: the most popular gun in American crime.
Like they're actually proud of that shit.
EOS

  puts quote
end

或者,不添加结束标签:

if render_quote?
  quote = "Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime. Do you believe that shit?
It actually says that in the little book that comes with it: the most popular gun in American crime.
Like they're actually proud of that shit."

  puts quote
end

或者,可选地,with a gsub to fix the identation-issues (yuk!?)。

连接。

if render_quote?
  quote =  "Now that there is the Tec-9, a crappy spray gun from South Miami."
  quote += "This gun is advertised as the most popular gun in American crime. Do you believe that shit?"
  quote += "It actually says that in the little book that comes with it: the most popular gun in American crime."
  quote += "Like they're actually proud of that shit."
  puts quote
end

(引用自Samuel L. Ipsum

我知道在我的脚本中包含这样的字符串(即视图逻辑)本身就是一种气味,但不知道有什么模式(除了 po-files 左右)来清理它。

【问题讨论】:

  • 您的连接与变量递增有何不同?您是否两次列出相同的内容?
  • @SergioTulentsev 这是我编造的名字:)。因为 foo++ 和 foo+= 是“增量”更新 var。你在这里也做同样的事情。

标签: ruby string messages heredoc


【解决方案1】:

请注意,相邻的字符串文字是连接在一起的。您可以将其与行连续字符 \ 结合使用。

if render_quote?
  quote =
  "Now that there is the Tec-9, a crappy spray gun from South Miami. " \
  "This gun is advertised as the most popular gun in American crime. " \
  "Do you believe that shit?" \
  "It actually says that in the little book that comes with it: " \
  "the most popular gun in American crime. " \
  "Like they're actually proud of that shit."
  puts quote
end

【讨论】:

  • 不知道为什么你不会在最后一个引号之前删除引号并简单地逃避返回。每一行都有你根本不需要的空格和引号。
  • @vgoff 我不知道你所说的“逃避回报”是什么意思。我在非最后一行之后添加了空格,以便下一行不会在标点符号之后立即连接。
  • 反斜杠是转义符,后面的字符是回车符。所以它逃脱了回报。如果您只是没有放置引号,将最后一个字符留在字符串中并使用反斜杠输入,那么输入最终会被“转义”并且不会被视为行的一部分。这能解释我在说什么吗?因此,使用转义的换行符,对于 Ruby 来说,它看起来像一堆同时放置的字符串,字符串之间有一个空格。
  • @vgoff 我明白你的意思。我没有意识到那部分。但是,如果没有多余的空格,您将无法缩进行。
  • 当然,你会的。你只是逃避回报......如果那是你想要的。如果那不是您想要的,并且您想保留回报,那就保留它们。你在这里使用它们的原因主要是告诉 Ruby 你还没有完成任务。所以它与缩进没有太大关系。这也是为什么我有点惊讶,如果“干净”的规定是一个衡量标准,那么答案是可以接受的。在这一点上,您基本上是在与 Ruby 战斗。
【解决方案2】:

您的代码在使用破折号之后对我不起作用......但这有效,不需要额外转义新行,并且只需说明它在 HereDoc 上所做的事情。

if render_quote?
  quote = <<-EOS.strip.split.join(' ')
    Now that there is the Tec-9, a crappy spray gun from South Miami.
    This gun is advertised as the most popular gun in American crime. Do you believe that shit?
    It actually says that in the little book that comes with it: the most popular gun in American crime.
    Like they're actually proud of that shit.
  EOS

  puts quote
end

EOS 前面的破折号表示我将能够以缩进的方式使用 EOS。

【讨论】:

  • 如果你不使用条带,它会确保你想要的缩进在那里。右侧没有额外的空格。感谢 Sawa 提到这一点。
【解决方案3】:

从 Ruby 2.3 开始,您可以使用波浪形的 heredoc 来避免字符串中的缩进,并且仍然在代码中保留缩进。

请参阅here 了解更多信息。

这里是该页面中的示例。

class Subscription
  def warning_message
    <<~HEREDOC
      Subscription expiring soon!
      Your free trial will expire in #{days_until_expiration} days.
      Please update your billing information.
    HEREDOC
  end
end

如果您不介意缩进,也可以像这样使用 %Q{} 语法,%Q 提供字符串替换,%q 不提供。

warning_message = %Q{
  Subscription expiring soon!
  Your free trial will expire in #{days_until_expiration} days.
  Please update your billing information.
}

【讨论】:

    猜你喜欢
    • 2010-09-27
    • 1970-01-01
    • 2014-09-18
    • 2016-09-12
    • 1970-01-01
    • 2016-06-22
    • 2011-09-26
    • 2016-07-15
    • 1970-01-01
    相关资源
    最近更新 更多