Chapter 3The Power of Procs and Lambdas

Recall that blocks by themselves are not objects—they cannot exist by themselves. In order to do anything interesting with a block, you need to pass a block into a method.

Procs have no such restrictions, because they are objects. They allow you to represent a block of code (anything between a do ... end) as an object. Some languages call these anonymous functions, and indeed, they do play the part.

Procs are ubiquitous in real-world Ruby code, although chances are, you might not be using them that much. Through the examples, you’ll learn how to use them effectively in your own code.

Ruby also uses Procs to perform some really nifty tricks. For example, have you ever wondered how ["o","h","a","i"].map(&:upcase) ...

Get Mastering Ruby Closures 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.