Welcome!
Welcome to the Ruby Closures book! You have taken a very important step into become a better Ruby developer.
Why this book?
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. I realized immediately that I had a major gap in my Ruby knowledge.
That incident left me unsure of my Ruby skills and knowledge. I decided to learn as much as I could about Ruby’s blocks. That pursuit also led me to procs and lambdas.
It didn’t take long to discover that blocks, procs, and lambdas are Ruby’s implementation of closures – hence the title of this book. The word “closure” sounds scary concept, and indeed, for most parts of my career, I avoided learning about it. Not this time. I got my hands on every book, conference video, screencast, and blog post that barely mentioned “closure” and dove in.
This book is the essence of everything I have learned.
Who is this book for?
This book is written for the Ruby (duh!) developer in mind. You are already comfortable with programming in Ruby but are by no means an expert.
This book will not go through the basics of Ruby, except when it comes it relates to the subject matter. There are many excellent books and resources that will do a much better job.
How to use this book
The overarching goal is to make you a better Ruby developer. To be a better developer, you have to learn how to read and write good code. This book is designed specifically to help you read and write good code that uses Ruby’s closures.
I have strived to make this book as understandable as possible. I have used numerous diagrams to make the explanations more concrete. You will find that I repeat concepts, although slightly different each time. I find that I learn much better this way, and I hope that works for you too.
Obviously, I shouldn’t be doing all the hard work. You will learn better when you try out the code samples on irb. More importantly, you will find exercises at the back of every chapter – DO THEM. Simply reading about code will not magically transform you into a better developer. Suggested solutions to all the exercises are included at the back of the book, so you will not be left in the dark if you are stuck.
One thing that have helped me improve as a developer is, as Gregory Brown from Practicing Ruby puts it, build cheap counterfeits. That is, we will implement our own versions of built-in features and scaled down versions of popular libraries, purely for educational purposes.
Structure of the book
This book is broken into four parts.
The first part covers the basics of closures and sets the groundwork for the rest of the book.
The second part dives into blocks, where we start with the basics and build our very own Enumerable and Enumerator in Ruby. You will learn about the patterns that involve blocks, and how they are used in real-world code.
The third part is dedicated to procs and lambdas. You will learn how to create and use them. You will also appreciate the special relationship that blocks, procs and lambdas have with each other. Doing the exercises will change how you write Ruby code.
Finally, the fourth part is when we put what we have learned into good use. We begin with building our own test framework à la RSpec. This will be our first taste in building a domain specific language, or DSL for short. We then extend our enumerables example and attempt to build lazy enumerables. We then tackle something slightly more complex by building a minimal Promises library in Ruby. If all that has left you scratching your head, no worries. All will be explained in due course.
About Ruby versions
I used Ruby 2.2.X to develop the examples. Ruby 1.9.X and 2.X should have no problems. If you have Ruby 1.8, you are on your own. Earlier versions of Ruby have confusing behavior (with regard to procs) that was fixed in later versions. Also, I’m too lazy (and you are too smart) to show you how to install Ruby.
Let’s do this!
I hope you are excited and ready to go! Warm up your brain and fire up your console, because we are diving straight into the fascinating world of closures.