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
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 ...