Ruby Academy · Lesson

Lambdas

Strict-arity procs.

Lesson 3 of 413 steps

Lambdas is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Strict-Arity Procs

A lambda is a special kind of Proc that behaves more like a regular method: it checks its argument count and treats return normally. Use it when you want predictable, method-like behavior.

square = lambda { |x| x * x }
puts square.call(6)

The Stabby Lambda

The arrow syntax ->, nicknamed the 'stabby lambda,' is the most common way to write lambdas. Parameters go in parentheses.

add = ->(a, b) { a + b }
puts add.call(4, 5)

Calling Lambdas

Lambdas support the same call styles as Procs: .call, .(), and [ ].

triple = ->(x) { x * 3 }
puts triple.call(2)
puts triple.(2)
puts triple[2]

Strict Argument Checking

Unlike a Proc, a lambda raises ArgumentError if you pass the wrong number of arguments. This catches bugs early.

greet = ->(name) { "Hi #{name}" }
puts greet.call("Sam")
begin
  greet.call
rescue ArgumentError => e
  puts "Error: #{e.message}"
end

return Stays Local

A return inside a lambda exits only the lambda, then execution continues. This matches how methods behave.

def demo
  l = -> { return 10 }
  l.call
  20  # this runs
end
puts demo

Checking for a Lambda

Both Procs and lambdas are instances of Proc. Ask lambda? to tell them apart.

l = ->(x) { x }
pr = proc { |x| x }
puts l.lambda?
puts pr.lambda?

Lambdas with No Arguments

Omit the parentheses when a lambda takes no parameters.

now = -> { Time.now.year }
puts "Year: #{now.call}"

Multi-line Lambdas

Lambdas can span multiple lines. The last expression is the return value.

classify = ->(n) {
  return "zero" if n.zero?
  n.positive? ? "positive" : "negative"
}
puts classify.call(0)
puts classify.call(-3)

Passing Lambdas to Methods

Like Procs, lambdas convert to blocks with &, so they slot into map, select, and friends.

doubler = ->(n) { n * 2 }
p [1, 2, 3].map(&doubler)

Lambdas as Return Values

A method can build and return a lambda, letting you create specialized functions on the fly.

def multiplier(by)
  ->(n) { n * by }
end
times10 = multiplier(10)
puts times10.call(5)

Putting It Together

Lambdas are strict, method-like Procs:

  • Write with lambda { } or ->(args) { }.
  • They enforce arity and keep return local.
  • Use them when correctness matters or you build functions to return.
is_even = ->(n) { n.even? }
p [1, 2, 3, 4, 5, 6].select(&is_even)

Quick Check

Test your understanding of lambdas.

Recap

You learned lambdas:

  • A lambda is a strict Proc; write it as ->.
  • It enforces argument counts.
  • return exits only the lambda.
  • lambda? distinguishes it from a plain Proc.
area = ->(w, h) { w * h }
puts area.call(3, 4)
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Lambdas” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Lambdas”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Lambdas”?

Strict-arity procs. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Lambdas” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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