You are previewing Eloquent Ruby.

Eloquent Ruby

Cover of Eloquent Ruby by Russ Olsen Published by Addison-Wesley Professional
  1. Title Page
  2. Copyright Page
  3. Praise for Eloquent Ruby
  4. Contents
  5. Foreword
  6. Preface
  7. Acknowledgments
  8. About the Author
  9. Part I. The Basics
    1. Chapter 1. Write Code That Looks Like Ruby
    2. Chapter 2. Choose the Right Control Structure
    3. Chapter 3. Take Advantage of Ruby’s Smart Collections
    4. Chapter 4. Take Advantage of Ruby’s Smart Strings
    5. Chapter 5. Find the Right String with Regular Expressions
    6. Chapter 6. Use Symbols to Stand for Something
    7. Chapter 7. Treat Everything Like an Object—Because Everything Is
    8. Chapter 8. Embrace Dynamic Typing
    9. Chapter 9. Write Specs!
  10. Part II. Classes, Modules, and Blocks
    1. Chapter 10. Construct Your Classes from Short, Focused Methods
    2. Chapter 11. Define Operators Respectfully
    3. Chapter 12. Create Classes That Understand Equality
    4. Chapter 13. Get the Behavior You Need with Singleton and Class Methods
    5. Chapter 14. Use Class Instance Variables
    6. Chapter 15. Use Modules as Name Spaces
    7. Chapter 16. Use Modules as Mixins
    8. Chapter 17. Use Blocks to Iterate
    9. Chapter 18. Execute Around with a Block
    10. Chapter 19. Save Blocks to Execute Later
  11. Part III. Metaprogramming
    1. Chapter 20. Use Hooks to Keep Your Program Informed
    2. Chapter 21. Use method_missing for Flexible Error Handling
    3. Chapter 22. Use method_missing for Delegation
    4. Chapter 23. Use method_missing to Build Flexible APIs
    5. Chapter 24. Update Existing Classes with Monkey Patching
    6. Chapter 25. Create Self-Modifying Classes
    7. Chapter 26. Create Classes That Modify Their Subclasses
  12. Part IV. Pulling It All Together
    1. Chapter 27. Invent Internal DSLs
    2. Chapter 28. Build External DSLs for Flexible Syntax
    3. Chapter 29. Package Your Programs as Gems
    4. Chapter 30. Know Your Ruby Implementation
    5. Chapter 31. Keep an Open Mind to Go with Those Open Classes
  13. Appendix. Going Further
  14. Index
  15. Footnotes
    1. Preface
    2. Acknowledgments
    3. Chapter 1
    4. Chapter 2
    5. Chapter 3
    6. Chapter 4
    7. Chapter 5
    8. Chapter 6
    9. Chapter 7
    10. Chapter 8
    11. Chapter 9
    12. Chapter 10
    13. Chapter 11
    14. Chapter 12
    15. Chapter 13
    16. Chapter 14
    17. Chapter 15
    18. Chapter 16
    19. Chapter 17
    20. Chapter 18
    21. Chapter 19
    22. Chapter 20
    23. Chapter 21
    24. Chapter 22
    25. Chapter 23
    26. Chapter 24
    27. Chapter 25
    28. Chapter 26
    29. Chapter 27
    30. Chapter 28
    31. Chapter 29
    32. Chapter 30
    33. Chapter 31
O'Reilly logo

Chapter 17. Use Blocks to Iterate

For programmers new to Ruby, code blocks are generally the first sign that they have definitely departed Kansas. Part syntax, part method, and part object, the code block is one of the key features that gives the Ruby programming language its unique feel. In fact, code blocks are kind of the Swiss Army Knife of Ruby programming; we use them for everything from initializing objects to building DSLs. In the next few chapters we will take a hard look at code blocks and the things you can do with them. The idea is to get beyond simply passing blocks to other people’s methods and move on to squeezing the last bit of utility out of the code blocks that get passed into your own methods.

We’re going to kick things off ...

The best content for your career. Discover unlimited learning on demand for around $1/day.