Proc.new
In this page:
Proc.new
A Proc wraps a block of code as a reusable, storable object that can be assigned to a variable and called later with .call. Proc.new { ... } (or the shorthand proc { ... }) creates one explicitly, unlike a plain block which only exists inline at a single call site. Procs are lenient about argument count -- passing too many or too few doesn't raise an error.
Note: A Proc silently ignores extra arguments and fills missing ones with nil, unlike a Lambda which enforces arity strictly.
Example: Proc.new
square = Proc.new { |x| x * x }
puts square.call(5)
puts square.(6)
puts square[7]
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: