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 26. Create Classes That Modify Their Subclasses

One of the fundamental principles of programming goes something like this: Never leave to a human that which can be done by a program. Or, to put it another way, some of the greatest leaps in software engineering have happened because some lazy sod simply got tired of repeating steps two through six over and over. So far in our adventures with Ruby’s open classes we have seen how we can change classes with monkey patching, a more or less manual process of slapping some new code over the existing code. From there we moved to building classes that could change themselves, classes that say “Gee, I’m running in Ruby 1.8, so I had better define this method.” In this chapter we’ll make the final ...

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