Back to library

💎Ruby Blocks, Procs, and Lambdas

Untangle Ruby's three callable forms one at a time — blocks, procs, and lambdas — until you can convert between them, predict every return-and-arity gotcha, and end by building a small block-taking DSL of your own.

Applied14 drops~2-week path · 5–8 min/daytechnology

Phase 1Blocks and the Hidden Contract of Every Ruby Method

Blocks, yield, and Ruby's hidden method contract.

4 drops
  1. Every Ruby method already accepts a block

    6 min

    Every Ruby method already accepts a block

  2. `yield` is just a method call with mysterious syntax

    6 min

    `yield` is just a method call with mysterious syntax

  3. Prefix a parameter with `&` to turn a block into an object

    7 min

    Prefix a parameter with `&` to turn a block into an object

  4. A block is syntax; a Proc is a value

    7 min

    A block is syntax; a Proc is a value

Phase 2Converting Between Blocks, Procs, and Lambdas

Convert fluently between blocks, procs, and lambdas.

5 drops
  1. A lambda is a Proc that takes its arguments seriously

    7 min

    A lambda is a Proc that takes its arguments seriously

  2. Four ways to create a callable, and they're not all equal

    6 min

    Four ways to create a callable, and they're not all equal

  3. Turning a block into a Proc is a one-character operation

    7 min

    Turning a block into a Proc is a one-character operation

  4. You can't unlambda a lambda — only create a fresh Proc

    7 min

    You can't unlambda a lambda — only create a fresh Proc

  5. `method(:name)` makes any method a first-class value

    7 min

    `method(:name)` makes any method a first-class value

Phase 3Arity, Return, and Closure Gotchas in Production

Navigate arity, return semantics, and closure capture.

4 drops
  1. Your Proc is about to silently drop half its arguments

    8 min

    Your Proc is about to silently drop half its arguments

  2. A `return` inside a Proc just escaped your entire method

    8 min

    A `return` inside a Proc just escaped your entire method

  3. Your callback captured the variable, not the value

    8 min

    Your callback captured the variable, not the value

  4. Reading a production stack trace with callables in it

    8 min

    Reading a production stack trace with callables in it

Phase 4Build a Small Block-Taking DSL

Build a block-taking DSL from scratch.

1 drop
  1. Ship a tiny config DSL that feels like native Ruby

    8 min

    Ship a tiny config DSL that feels like native Ruby

Frequently asked questions

What's the difference between a proc and a lambda in Ruby?
This is covered in the “Ruby Blocks, Procs, and Lambdas” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Why does return behave differently in a proc versus a lambda?
This is covered in the “Ruby Blocks, Procs, and Lambdas” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
When should I use yield instead of a block argument?
This is covered in the “Ruby Blocks, Procs, and Lambdas” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
How does &block actually work in a method signature?
This is covered in the “Ruby Blocks, Procs, and Lambdas” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What is a Ruby DSL and how do blocks enable it?
This is covered in the “Ruby Blocks, Procs, and Lambdas” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.