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

Benjamin Tan Wei Hao

  • 出版商: Pragmatic Bookshelf
  • 出版日期: 2017-08-26
  • 定價: $900
  • 售價: 8.0$720
  • 語言: 英文
  • 頁數: 96
  • 裝訂: Paperback
  • ISBN: 1680502611
  • ISBN-13: 9781680502619
  • 相關分類: Ruby
  • 立即出貨(限量) (庫存=4)



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.


成為 Ruby 高手的道路上,充滿了 blocks、procs 和 lambdas。要成為一個真正有效的 Ruby 程式設計師,僅僅了解這些特性是不夠的 - 你需要知道如何在實踐中使用它們。探索如何撰寫優雅、表達力強且令人愉悅的程式碼,並深入了解這些概念,以便更輕鬆地使用第三方的 gems 和 libraries。無論你是完全新手還是經驗豐富的 Rubyist,你都會在你的程式碼中找到這些概念的好用之處。

Ruby 開發者將 Ruby 語言視為表達性、靈活性和優雅性的標準 - 而其中很大一部分歸功於 blocks、lambdas 和 procs。這些語言特性使 Ruby 成為最美麗和最愉悅的程式語言之一。學習如何理解和撰寫能讓你更接近 Ruby 高手的程式碼。

從閉包的基礎開始,然後深入研究 blocks,了解涉及 blocks 的模式以及它們在實際程式碼中的使用方式。然後創建並使用 procs 和 lambdas。最後,使用高級的 Ruby 功能(如 fibers 和 generators)建立自己的惰性可枚舉物件。在此過程中,與計算機科學概念(如閉包、自由變數和一級函數)一起工作。輕鬆辨識閉包。識別可以有效使用 blocks 的模式,並使用它們重新實現常見的標準庫方法。追蹤 Symbol#to_proc 的運作方式,並了解調用 procs 的不同方法。這本簡潔的指南通過引人入勝的例子介紹了不同類型的 Ruby 閉包,每個章節都附有測試和挑戰你理解能力的練習題。

當你完成這本書時,blocks、procs 和 lambdas 將成為你 Ruby 工具箱中不可或缺的一部分。

你需要準備以下兩項:Ruby 2.x 和對 Ruby 的基本熟悉。