0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】文字列の中に式や変数を埋め込みたい

Last updated at Posted at 2021-01-27

文字列の中に式を埋め込みたい時

変数の値などを含む文字列を表示したい時、
Rubyでは文字列中に#{}で囲んだ式を埋め込むとスクリプト実行時にその式の評価した結果に置き換えられます。

sample = "sample_str"

message = "#{sample}さん、おはよう!" # => "sample_strさん、、おはよう!"

また数値(int)を代入した場合は計算結果に置き換えられます。

int1 = 5
int2 = 2

message = "5+2 = #{int1 + int2} です!" # => "5+2 = 7 です!"

まとめ

Rubyの「式展開」についてまとめました。
簡単ですけどよく使います。

Ruby 2.7.0 リファレンスマニュアル
https://docs.ruby-lang.org/ja/2.7.0/doc/spec=2fliteral.html#exp

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?