Ruby (クライアント スクリプト) を書いているとき、行末を含む長い文字列を作成する方法が 3 つあります。
よりクリーンでより良い方法はありますか?
可変インクリメント。
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 =<<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
(サミュエル・L・イプサムからの引用)
スクリプトにそのような文字列 (つまりビュー ロジック) があること自体が臭いことは承知していますが、これをクリーンアップするためのパターン (po ファイルなど以外) を知りません。