O'Reilly logo

Mastering Ruby Closures by Benjamin Tan Wei Hao

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required