6

在编写 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

或者,可选地,使用 gsub 来修复标识问题(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 左右)来清理它。

4

3 回答 3

5

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

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
于 2012-11-13T12:22:36.497 回答
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。

于 2012-11-13T14:04:30.713 回答
2

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

请参阅此处了解更多信息。

这是该页面中的示例。

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.
}
于 2018-11-06T17:28:12.597 回答