1
1

More than 3 years have passed since last update.

Procの実行方法いろいろ

Last updated at Posted at 2019-10-08

Procに引数を渡して処理を実行する方法はいくつかあります。
以下は全て同じ結果を返します。


proc = Proc.new do |a|
  puts "This is proc: #{a}"
end

# オーソドックスな記法
proc.call(1)

# callを省いたシンタックスシュガーな記法
proc.(2)

# 添字で引数を指定して呼び出す記法
proc[3]

# ===で引数を指定して呼び出す記法
# ===が実装されていることでwhen句に手続きを渡せるようになっている
proc === 4

出力結果


This is proc: 1
This is proc: 2
This is proc: 3
This is proc: 4
1
1
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
1
1