18
16

More than 5 years have passed since last update.

rubyの式展開

Posted at

ruby の式展開の仕方をまとめました。一番下のやつをさっき知ってびっくりしたので、忘れぬうちにメモ

test.rb
hoge = "__hoge__"

# 基本的な式展開
puts "#{hoge}" #=> __hoge__

# 式展開の中では普通にコードがかける
puts "#{hoge + hoge.upcase}" #=> __hoge____HOGE__

# printf 風
puts "%d" % 12345 #=> 12345

# 複数の場合はArrayで
puts "%d, %d" % [12345, 56789] #=> 12345, 56789

# ハッシュも渡せる
puts "%{hoge}, %{fuga}" % {:fuga => "__fuga__", :hoge => "__hoge__" } #=> __hoge__, __fuga__
18
16
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
18
16