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

Proc.new

A Proc wraps a block into a real object you can save in a variable and run whenever you want. It is like recording instructions on a card you can keep and reuse.

In this page:

  1. Proc.new
Syntax
ruby
proc_name = Proc.new { |param| expression }
proc_name.call(argument)

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]
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a Proc with the wrong number of arguments and expecting an error, when extra arguments are ignored and missing ones become nil.
  2. Using return inside a Proc, which returns from the enclosing method and can raise LocalJumpError at top level.
  3. Forgetting .call, .() or [], so the Proc is never run.
🔒

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.