KDOC 708: Rubyのlambdaは引数の数を検査するがprocはしない

この文書のステータス

  • 作成
    • 2026-10-18 貴島
  • レビュー
    • 2026-10-18 貴島

概要

Rubyの lambda と proc はどちらもProcクラスのオブジェクトだが、引数の扱いが違う。 lambda はメソッドと同じく引数の数を検査し、合わないと ArgumentError になる。 proc は余った引数を捨て、足りない引数を nil にして動く。

In lambdas, arguments are treated in the same way as in methods: strict, with ArgumentError for mismatching argument number, and no additional argument processing; Regular procs accept arguments more generously: missing arguments are filled with nil, single Array arguments are deconstructed if the proc has multiple arguments, and there is no error raised on extra arguments.

Proc (Lambda and non-lambda semantics) - Ruby 3.4

l = lambda { |a, b| [a, b] }
pr = proc { |a, b| [a, b] }
puts "クラス: #{l.class} / #{pr.class}"
puts "proc に1つ: #{pr.call(1).inspect}、3つ: #{pr.call(1, 2, 3).inspect}"
begin
  l.call(1)
rescue ArgumentError => e
  puts "lambda に1つ: #{e.class}"
end
クラス: Proc / Proc
proc に1つ: [1, nil]、3つ: [1, 2]
lambda に1つ: ArgumentError

関連

なし。