Mastering Ruby Closures
A Guide to Blocks, Procs, and Lambdas
by Benjamin Tan Wei Hao
Version: P1.0 (August 2017)
Copyright 2017 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/btrubyclo, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher) Janet Furlow (VP of Operations) Susannah Davidson Pfalzer (Executive Editor) Brian P. Hogan (Development Editor) Nicole Abramowitz (Copy Editor) Gilson Graphics (Layout)
For customer support, please contact .
For international rights, please contact .
To my parents, Virginia and Michael, and to my favorite girls in the world, Hui Ling and Gu Gu.
Table of Contents
Copyright 2017, The Pragmatic Bookshelf.
Early Praise for Mastering Ruby Closures
This is an excellent book for experienced Ruby developers to add functional programming paradigms to their toolkit. This book gives excellent examples of blocks, Proc s, and lambdas and ways to use them in your existing application.
Jeffrey Holland |
Senior software engineer, Ackmann & Dickenson |
Ive been a Rubyist for over a decade, and I still learned several new techniques and how to avoid gotchas with Proc s, 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 Rubys closures. Easy to understand, interesting examples, and a solid reference.
Matthew Margolis |
Director, software engineering, Getty Images |
Youve probably called methods that take in a block, but if youve not written a method that takes in a block, I highly recommend this book to you. Once youre through with the book, youll be able to explain how [1, 2, 3].map(&:to_s) works with confidence. Ive always been a fan of Benjamins 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! Dont be afraid, and take a look yourself!
Gbor Lszl Hajba |
Software engineer, Zhlke Engineering |
Acknowledgements
Even though this is my second published book, the sheer amount of work that goes into taking the book from conceptualization to the final product still amazes me, even for a short book like this. I need to thank a bunch of people who suffered with me through this arduous journey:
My Leanpub Readers:
This book originally started life on Leanpub. To all the readers who bought version 1.0, thank you for your patience and kind words. I hope it was worth the wait.
The Publishers:
Susannah Pfalzer and Brian Hogan, thank you for taking a chance on me and believing in the book. It is such an honor to be part of the Prag family. Without your gentle prodding, this project would have taken much, much longer. More importantly, thank you for making me a much better author.
The Reviewers:
Rohit Arondekar, Chad Dumler-Montplaisir, Jeff Holland, Gbor Lszl Hajba, Matthew Margolis, and Kim Shrieryour sharp eyes have saved me from many embarrassing mistakes. If any one of you ever write a book, Ill return the favor.
My Wife:
The idea for this book came to me while I was waiting for you to come out of the restroom during our honeymoon in Taiwan. Without that, this book would never have been written. The support that you gave me could never be overstated. I cannot promise that I will not write a third book...
My Parents:
As usual, thanks for everything. Although you might not understand anything in the book, this was written for you both.
Copyright 2017, The Pragmatic Bookshelf.
Welcome!
Welcome to the Ruby closures book! You have taken a very important step toward becoming a better Ruby developer.
Closures are used everywhere in Ruby, from simple scripts to the most popular web frameworks. You might have even been using closures without knowing it.
However, once you become familiar with closures and their features, they will start jumping out at you, and youll appreciate the ways in which closures enable expressive and beautiful programs. But closures do more than that, especially in Ruby. Closures allow you to extend the language by enabling you to write your own domain-specific languages, or DSLs.