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

在编写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来修复标识 -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文件之外)以清除它。

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

 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 

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

你的代码之后用破折号不能为我工作……但是这样做有效,不需要额外转换新行,只是简单地说明它在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。