← Back to Ruby Course | Chapter 8: Blocks, Procs & Lambdas | Lesson 3 of 6

Proc.new

In this page:

  1. Proc.new

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

markup
square = Proc.new { |x| x * x }

puts square.call(5)
puts square.(6)
puts square[7]
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.