Implementing Callbacks in Ruby with Lambdas

At times, closures allow us to write programs that are otherwise very difficult to express or downright nasty to look at. An example of this is callbacks. Imagine that you’re working with a report-generation tool. The programmer before you hacked together something quick and dirty and has since left for greener pastures. Lucky him. Unfortunately for you, the report-generating method has a couple of bugs, causing crashes to occur 5% of the time.

The code is a complete mess and you have no appetite to go near that monstrosity. Instead, you want to know if a report has been successfully generated and send it to your boss right away. However, when things go awry, you want to be notified personally. ...

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.