0Pricing
Ruby Academy · Lesson

Procs

Reusable block objects.

Reusable Block Objects

A block is not an object on its own, so you cannot store it in a variable. A Proc wraps a block into a real object you can save, pass around, and call later.

greeter = Proc.new { puts "Hello!" }
greeter.call
greeter.call

Creating Procs

Two common ways to build a Proc: Proc.new { ... } and the shorter proc { ... } Kernel method. Both produce the same kind of object.

double = proc { |x| x * 2 }
p double.call(5)

All lessons in this course

  1. Blocks Recap
  2. Procs
  3. Lambdas
  4. Closures and Scope
← Back to Ruby Academy