Chapter 8. How Ruby Borrowed a Decades-Old Idea from Lisp

image with no caption

Blocks are Ruby’s implementation of closures.

Blocks are one of the most commonly used and powerful features of Ruby because they allow you to pass a code snippet to Enumerable methods, such as each, detect, or inject. Using the yield keyword, you can also write your own custom iterators or functions that call blocks for other reasons. Ruby code containing blocks is often more succinct, elegant, and expressive than equivalent code in older languages, such as C.

But don’t jump to the conclusion that blocks are a new idea! In fact, blocks are not new to Ruby at all. The computer science concept ...

Get Ruby Under a Microscope 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.