The Ruby Closures Book

The Ruby Closures Book

Everything You Need to Master Blocks, Procs and Lambdas

About the Book

NOTE: This book will go under the Pragmatic Programmers' imprint! Stay tuned!

I have been a Ruby programmer for quite a number of years, but until recently, I have been blissfully ignorant of one of Ruby's most powerful features. That is, until one day, I saw a co-worker casually write a method that took in a block. Holy ****! I realized at that moment, I had a major gap in my Ruby knowledge.

Sure, I knew how to use blocks, and vaguely knew how to use lambdas in Rails scopes. But who was I kidding? I was never going to be a competent Ruby programmer if I didn't fix this.

Here's the thing. I spoke to a few of my Ruby programmer friends, and guess what, the situation is the same. If you are like that then this book is for you.

This book will help you gain a deep understanding of Ruby's closures, and more importantly, how to harness its power.

What you will learn:

  • What are closures?
  • Why and how are closures useful?
  • The differences between lambdas and procs
  • Ruby patterns using blocks
  • Real-world examples of closures
  • Implement your own testing framework, lazy enumerables and DSLs
  • And much more ...

The Story Behind the Book

I was honeymooning with my wife in Taiwan. As I was waiting for her outside the bathroom up at some mountain, it dawned upon me that "www.closur.es" would make for a fantastic domain name. But since that was too general, why not "www.rubyclosur.es" instead? I congratulated myself on being an absolute genius, and immediately proceeded to buy that domain. I began writing the book the very next day. Thankfully, my wife didn't complain.

About the Author

Benjamin Tan Wei Hao
Benjamin Tan Wei Hao

Ohai! I'm Benjamin. I am a software engineer at Neo Innovation. I absolutely love Ruby. Even after using it for years, I still find so much more to learn. You can read some of my articles on Ruby on SitePoint.

I live in Singapore with my wonderful wife. I am currently working on another book. (If you are into Elixir, you'll love it.)

If you have any questions at all, I'm an email away.

Table of Contents

  • Welcome!
    • Why this book?
    • Who is this book for?
    • How to use this book
    • Structure of the book
    • About Ruby versions
    • Let’s do this!
  • Closing in on Closures
    • Closures made to sound hard
    • Closures are not hard
    • Lexical Scoping: Closest one wins
    • Closures, finally!
    • Doing the Lambda
    • Using Lambdas to build other Lambdas
    • First-class functions
    • Summary
    • Exercises
    • Solutions
  • Beautiful Blocks
    • yield and block_given?
    • yield semantics
    • Argument passing
    • Blocks for enumeration
    • Implementing times
    • Implementing each
    • Exercises
    • Solutions
    • Managing Resources with Blocks
    • Implementing File.open
    • Exercises
    • Solutions
    • Making object initialization beautiful
    • Implementation
    • Creating DSLs with blocks
    • Using instance_eval to change self
    • Exercises
    • Solutions
    • Blocks are closures
    • Block local variables
    • Block variables act like parallel assignment
    • Summary
  • Proctology
    • The four ways of calling procs
    • Lambdas
    • Stabby lambda syntax
    • How does Symbol#to_proc work?
    • Exercises
    • Solutions
    • Lambdas – same same, but different
    • The difference between a lambda and a proc
    • Lambdas or procs?
    • Exercises
    • Solutions
    • Currying with Procs
    • TODO: The Relationship between Blocks and Procs
  • Cheap Counterfeiting
    • Implementing Lazy Enumerables
    • Building the Skeleton
    • External VS Internal Iteration
    • Creating an Enumerator from an Enumerable
    • Generators, Fibers, oh my!
    • Implementing Lax, our own Enumerator::Lazy
    • Implementing Lazy map
    • Implementing Lazy take
    • Summary
    • Exercises
    • Solutions
  • Epilogue

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub