Blocks, procs and lambdas (also known as closures in other languages) are one of the most powerful constructs in Ruby. There are a variety of different ways to create and use blocks, which this workshop aims to clarify.
Coding walk-through to be done in person with group.
- Implicit blocks
- Block arguments
- Multiple yields
- Block presence
- Block scopes
- Block objects
- Explicit blocks
- Return values
- Procs are objects, blocks are not
- Blocks are part of the syntax of a method call
- Both
proc
andlambda
return Proc objects - Lambdas assert arity (number of passed arguments)
- Return inside a lamba returns flow to the outer code scope
- Return inside a proc is executed in the outer scope (as if inside that scope directly)