LoginSignup
2
2

More than 5 years have passed since last update.

Rubyで文字列の入った変数を処理するときはとりあえず"#{}"で展開とするとよさそう

Posted at

文字列の入った変数を処理するときに、nil が入っていることが避けられないことがある。
うっかり、

str += tmp

という書き方をすると、tmp が nil だったときに例外が出て困るので、

str += "#{tmp}"

と文字列リテラル中で展開した方がよさそう。
同様に、

str = "#{tmp_1 + tmp_2 + tmp_3}"

よりも、

str = "#{tmp_1}#{tmp_2}#{tmp_3}"

がよさそう。

2
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2