Procs & Lambdas

Ruby is a dynamic, beginner-friendly programming language, and procs and lambdas let you store a chunk of code in a variable — turning behaviour itself into data you can pass around and call later.

Learn Procs & Lambdas in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll create procs and lambdas, call them, understand the differences, and convert them back into blocks with & .

What You'll Learn in This Lesson

1️⃣ Creating and Calling Procs and Lambdas

A block isn't an object — but a Proc is. Build one with Proc.new {' '} and run it with .call . A lambda is a stricter cousin, written most cleanly with the stabby -> syntax. Because both are objects, you can store them in arrays and hashes.

2️⃣ How They Differ (Arguments and return)

Two real differences matter. A lambda is strict about argument count; a proc is lenient. And return inside a lambda exits only the lambda, while inside a proc it tries to exit the surrounding method. The & operator converts a stored callable into a block when a method expects one.

Your turn. Build a calculator from lambdas stored in a hash. A :divide lambda is added for you — complete the TODO calls and predict the output, then run it.

Create a lambda and apply it across an array with map(&lambda) — the bridge from callable to block. Run with ruby discount.rb .

📋 Quick Reference — Procs & Lambdas

Practice quiz

How do you run a stored Proc or lambda?

  • call it directly like a method
  • with .run
  • with .call
  • with .exec

Answer: with .call. Use .call to invoke a Proc or lambda, e.g. square.call(4).

Which is the modern stabby-lambda syntax?

  • ->(n) { n }
  • lambda { |n| n }
  • proc { |n| n }
  • def(n) { n }

Answer: ->(n) { n }. The stabby form ->(n) { n } is the modern way to write a lambda.

How does a lambda treat the wrong number of arguments?

  • Ignores extras, nil for missing
  • Returns nil
  • Converts them to an array
  • Raises ArgumentError

Answer: Raises ArgumentError. Lambdas are strict about argument count and raise ArgumentError on a mismatch.

How does a Proc treat a missing argument?

  • Raises ArgumentError
  • Binds it to nil
  • Returns false
  • Skips the call

Answer: Binds it to nil. Procs are lenient: a missing argument simply becomes nil, no error raised.

Where does return jump inside a lambda?

  • Exits only the lambda
  • Exits the enclosing method
  • Raises an error
  • Exits the whole program

Answer: Exits only the lambda. return inside a lambda exits only the lambda, so the surrounding method continues.

Which are all valid ways to call a lambda named sq?

  • sq(4)
  • sq->4

sq.call(4), sq.(4), and sq[4] all invoke the lambda.

What does the & do in arr.map(&my_lambda)?

  • Doubles the lambda
  • Converts the lambda into a block
  • Calls it twice
  • Negates it

Answer: Converts the lambda into a block. & converts a stored callable into the block the method expects.

Is a bare block (without storing it) an object you can pass around?

  • Yes, always
  • Only with lambda
  • Only in Ruby 3
  • No, you must wrap it in a Proc/lambda first

Answer: No, you must wrap it in a Proc/lambda first. A block isn't an object; wrap it in a Proc or lambda to store and pass it.

What does ->(n){ n * n }.call(4) return?

  • 8
  • 16
  • 4
  • nil

Answer: 16. The lambda squares its argument, so calling it with 4 returns 16.

Why are lambdas often preferred over procs?

  • They run faster always
  • They use less memory
  • Strict args and predictable return catch bugs earlier
  • They can't be stored

Answer: Strict args and predictable return catch bugs earlier. Lambdas' strict argument checking and local return behaviour make them safer.