Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中构建长字符串的干净方法_Ruby_String_Messages_Heredoc - Fatal编程技术网

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

在Ruby中构建长字符串的干净方法,ruby,string,messages,heredoc,Ruby,String,Messages,Heredoc,在编写Ruby(客户端脚本)时,我看到了三种构建更长字符串的方法,包括行尾,所有这些对我来说都有点难闻 有没有更干净更好的方法 变量正在递增。 Heredocs(和未关闭的引号)。 或者,可以选择,-问题(yuk!?) 连接。 (引自) 我知道在我的脚本中有这样的字符串(即视图逻辑)本身就是一种味道,但我不知道有什么模式(除了po文件之类的)来清理它。注意,相邻的字符串文字是串联的。您可以将其与连续字符\结合使用 if render_quote? quote = "Now that th

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

有没有更干净更好的方法

变量正在递增。 Heredocs(和未关闭的引号)。 或者,可以选择,-问题(yuk!?)

连接。 (引自)


我知道在我的脚本中有这样的字符串(即视图逻辑)本身就是一种味道,但我不知道有什么模式(除了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

你的密码对我以后的破折号无效。。。但这是可行的,不需要额外转义新行,只需说明它在herdoc上的作用

if render\u quote?

quote=自从Ruby 2.3以来,您使用了squiggly Herdeoc来避免字符串中的缩进,并且仍然在代码中保留缩进

有关更多信息,请参阅

下面是该页中的示例

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

连接与变量递增有何不同?你把同一件事列了两次了吗?@SergioTulentsev这是我虚构的名字:)。因为foo++和foo+=是“增量”更新变量。在这里您也会这样做。不知道为什么您只是在最后一个引号之前不删除引号,而只是逃避返回。每行都有空格和引号,这是你根本不需要的。@vgoff我不知道你说的“转义返回”是什么意思。我在非最后一行之后添加了空格,这样下一行就不会在标点符号后面连接。反斜杠是转义,后面的字符是返回。因此,它逃避了回报。如果您只是没有放置引号,在字符串中留下最后一个字符并使用反斜杠enter,那么enter将被“转义”,并且不会被视为行的一部分。这能解释我说的话吗?所以对于转义换行符,对Ruby来说,它看起来像是一组同时放置的字符串,字符串之间有一个空格。@vgoff我明白你的意思了。我不知道那部分。但是,如果没有多余的空格,您将无法缩进行。当然,您可以。你只是在逃避返回。。。如果这是你想要的。如果这不是你想要的,你想保留回报,那么就保留它们。您在这里使用它们的原因主要是告诉Ruby您还没有完成任务。所以这和缩进没有多大关系。这也是为什么我有点惊讶,如果“干净”的规定是一个衡量标准,答案是接受。在这一点上,您基本上是在与Ruby进行斗争。如果您不使用条带,它将确保缩进在您想要的地方。右侧没有额外的空间。谢谢Sawa提到这一点。
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
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
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
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
warning_message = %Q{
  Subscription expiring soon!
  Your free trial will expire in #{days_until_expiration} days.
  Please update your billing information.
}