CHAPTER 12

image

Blocks, Procs, and Lambdas

Some of the most powerful features of the Ruby language are closures, which are known as blocks, procs, and lambdas within Ruby. Closures within Ruby are also often misunderstood, mainly due to the flexibility when using them within Ruby.

A closure is in essence a function that can be stored as a variable; closures within Ruby are mainly used for iterating through data/result sets. Closures allow external access to local variables from within the closure.

Blocks

The simplest of the three closures is a block; this follows Ruby’s “normal” programming style. Blocks are simply “bits” of code that can be executed. ...

Get Ruby Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.