0%

Book Description

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 completely new to Ruby or a seasoned Rubyist, you'll find good use for these concepts in your code.

Ruby developers use the Ruby language as the yardstick for expressivity, flexibility, and elegance - and a large part of this is due to blocks, lambdas, and procs. These language features make Ruby one of the most beautiful and pleasant languages to work with. Learn how to understand and craft code that will take you closer to Ruby mastery.

Start with the basics of closures and then dive into blocks, as you learn about the patterns that involve blocks, and how they are used in real-world code. Then create and use procs and lambdas. Finally, build your own lazy enumerables with advanced Ruby features such as fibers and generators. Along the way, work with computer science concepts such as closures, free variables and first-class functions. Spot a closure easily. Identify the patterns where you can effectively use blocks and re-implement common standard library methods using them. Trace through how Symbol#to_proc works and find out the different ways of calling procs. This succinct guide takes you through the different kinds of Ruby closures with engaging examples, and each chapter comes with exercises that test and challenge your understanding.

When you finish this book, blocks, procs, and lambdas will have become an integral part of your Ruby toolbox.

What You Need:Ruby 2.x and a basic familiarity with Ruby.

Table of Contents

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