在Ruby中构建长字符串的干净方法

时间:2012-11-13 11:11:33

标签: ruby string messages heredoc

在编写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 - 问题(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文件之外的其他模式)来清理它。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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。

答案 2 :(得分:2)

从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.
}