ber*_*kes 5 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
或者,只是不添加结束标记:
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
Run Code Online (Sandbox Code Playgroud)
或者,可选地,使用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
Run Code Online (Sandbox Code Playgroud)
(引自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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
835 次 |
| 最近记录: |