ebook img

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

92 Pages·2017·2.228 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

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

Early Praise for Mastering Ruby Closures This is an excellent book for experienced Ruby developers to add functional pro- gramming paradigms to their toolkit. This book gives excellent examples of blocks, Procs, and lambdas and ways to use them in your existing application. ➤ Jeffrey Holland Senior software engineer, Ackmann & Dickenson I’ve been a Rubyist for over a decade, and I still learned several new techniques and how to avoid gotchas with Procs, 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 Ruby’s closures. Easy to understand, interesting exam- ples, and a solid reference. ➤ Matthew Margolis Director, software engineering, Getty Images You’ve probably called methods that take in a block, but if you’ve not written a method that takes in a block, I highly recommend this book to you. Once you’re through with the book, you’ll be able to explain how [1,2,3].map(&:to_s) works with confidence. I’ve always been a fan of Benjamin’s 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! Don’t be afraid, and take a look yourself! ➤ Gábor László Hajba Software engineer, Zühlke Engineering Mastering Ruby Closures A Guide to Blocks, Procs, and Lambdas Benjamin Tan Wei Hao The Pragmatic Bookshelf Raleigh, North Carolina 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, PragProg and the linking g device are trade- marks 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. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Publisher: Andy Hunt VP of Operations: Janet Furlow Executive Editor: Susannah Davidson Pfalzer Development Editor: Brian P. Hogan Copy Editor: Nicole Abramowitz Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2017 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-68050-261-9 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—August 2017 To my parents, Virginia and Michael, and to my favorite girls in the world, Hui Ling and Gu Gu. Contents Acknowledgements . . . . . . . . . . . ix Welcome! . . . . . . . . . . . . . xi 1. Closing in on Closures . . . . . . . . . . 1 The Foundations of a Closure 1 Simulating Classes with Closures 5 Implementing Callbacks in Ruby with Lambdas 7 Implementing Enumerable#reduce() (or Fold Left) 11 Test Your Understanding! 15 Wrapping Up 16 2. Beautiful Blocks . . . . . . . . . . . . 17 Separating the General from the Specific 18 Executing Blocks with the yield Keyword 19 Blocks as Closures and Block Local Variables 21 Block Pattern #1: Enumeration 22 Block Pattern #2: Managing Resources 26 Block Pattern #3: Beautiful Object Initialization 29 Test Your Understanding! 37 Wrapping Up 39 3. The Power of Procs and Lambdas . . . . . . . 41 Procs and the Four Ways of Calling Them 42 Lambdas—Same, But Different 44 How Symbol#to_proc Works 47 Currying with Procs 52 Test Your Understanding! 55 Wrapping Up 56 4. Building Your Own Lazy Enumerables . . . . . . 57 Understanding Lazy Enumerables 57 Contents • viii Building Our Skeleton 59 Implementing Lax 63 Implementing Lazy map() 66 Implementing Lazy take() 67 Test Your Understanding! 69 Wrapping Up 70 A1. Solutions . . . . . . . . . . . . . 71 Chapter 1 71 Chapter 2 72 Chapter 3 74 Chapter 4 74 Bibliography . . . . . . . . . . . . . 77

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.