small medium large xlarge

Mastering Ruby Closures: A Guide to Blocks, Procs, and Lambdas


Cover image for Mastering Ruby Closures

Mastering Ruby Closures

A Guide to Blocks, Procs, and Lambdas


The road to Ruby mastery is paved with blocks, procs, and lambdas. To be a truly effective Ruby programmer, it’s not enough just to understand these features—you need to know how to use them in practice. Discover how to write code that is elegant, expressive, and a joy to use, and gain a deep understanding of these concepts so you can work with third-party gems and libraries more easily. Whether you are an intermediate or a seasoned Rubyist, you’ll find good use for these concepts in your code.

Customer Reviews

This is an excellent book for experienced Ruby developers to add functional programming
paradigms to their toolkit. This book gives excellent examples of blocks,
Procs, and lambdas and ways to use them in your existing application.

- Jeffrey Holland

Senior software engineer, Ackmann & Dickenson

I’ve been a Rubyist for over a decade, and I still learned several new techniques
and how to avoid gotchas with Procs, lambdas, and blocks. This book is also perfect
for the Ruby/Rails developer who wants to feel confident when consuming or
writing libraries that use Ruby’s closures.

- Matthew Margolis

Director, software engineering, Getty Images

You’ve probably called methods that take in a block, but if you’ve not written a
method that takes in a block, I highly recommend this book to you. I’ve always been a fan of Benjamin’s writing because every concept is always followed
up by real-world examples, which makes it easier for the reader to grok the
concepts being taught.

- Tan Guo Xiang

Software engineer, Civilized Discourse Construction Kit

I never heard Ruby could do closures, but Mastering Ruby Closures brought me from zero to hero in just a few hours! Don’t be afraid, and take a look yourself!

- Gábor László Hajba

Software engineer, Zühlke Engineering

See All Reviews

What You Need

Ruby 2.x and a familiarity with intermediate Ruby concepts.

Contents & Extracts

  • Acknowledgements
  • Welcome!
    • Why This Book?
    • Who Is This Book For?
    • How to Use This Book
    • What’s in This Book
    • About Ruby Versions
    • Where to Go for Help
  • Closing in on Closures
    • The Foundations of a Closure
    • Simulating Classes with Closures
    • Implementing Callbacks in Ruby with Lambdas
    • Implementing Enumerable#reduce (or Fold Left)
    • Test Your Understanding!
    • Wrapping Up
  • Beautiful Blocks excerpt
    • Separating the General from the Specific
    • Executing Blocks with the yield Keyword
    • Blocks as Closures and Block Local Variables excerpt
    • Block Pattern #1: Enumeration
    • Block Pattern #2: Managing Resources
    • Block Pattern #3: Beautiful Object Initialization
    • Test Your Understanding!
    • Summary
  • The Power of Procs and Lambdas excerpt
    • Procs and the Four Ways of Calling Them
    • Lambdas—Same, But Different
    • How Symbol#to_proc Works
    • Currying with Procs
    • Test Your Understanding!
    • Wrapping Up
  • Building Your Own Lazy Enumerables
    • Understanding Lazy Enumerables
    • Building Our Skeleton
    • Implementing Lax
    • Implementing Lazy map
    • Implementing Lazy take
    • Wrapping Up
    • Exercises
  • Solutions
    • Chapter 1
    • Chapter 2
    • Chapter 3
    • Chapter 4


Benjamin Tan Wei Hao is the author of The Little Elixir and OTP Guidebook and has spoken at Ruby conferences about Elixir. When he’s not reading, writing, or playing with his daughter Audrey, you will find him developing awesome software at Pivotal Labs, Singapore. He blogs at and you can follow him on Twitter @bentanweihao.