8
4

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 5 years have passed since last update.

Ruby と JavaScript の Template literal (文字列展開、式展開)

Last updated at Posted at 2019-04-22

Ruby

Double quotes + #{} で展開できる。

era_name = '令和'
puts "次の元号は...#{era_name}です!" # 次の元号は...令和です!

Single quotes で囲むと展開されない。

era_name = '令和'
puts '次の元号は...#{era_name}です!' # 次の元号は...#{era_name}です!

Back quotes で囲むとコマンドとして実行されるのでエラーになる。

era_name = '令和'
puts `次の元号は...#{era_name}です!` # No such file or directory - 次の元号は...令和です! (Errno::ENOENT)

JavaScript

Back quotes + ${} で展開できる。

const eraName = '令和';
console.log(`次の元号は...${eraName}です!`); // 次の元号は...令和です!

Single quotes, Double quotes で囲むと展開されない。

const eraName = '令和';
console.log('次の元号は...${eraName}です!'); // 次の元号は...${era_name}です!
console.log("次の元号は...${eraName}です!"); // 次の元号は...${era_name}です!

まとめ

  • Ruby で文字列展開したい時は、Double quotes + #{}
  • JavaScript で文字列展開したい時は、Back quotes + ${}
8
4
1

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
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?