0Pricing
Ruby Academy · Lesson

Lambdas

Strict-arity procs.

Lambdas is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Frequently asked questions

Is the “Lambdas” lesson free?

Yes — the full text of “Lambdas” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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