Effective Ruby: 48 Specific Ways to Write Better Ruby

Book description

If you’re an experienced Ruby programmer, Effective Ruby will help you harness Ruby’s full power to write more robust, efficient, maintainable, and well-performing code. Drawing on nearly a decade of Ruby experience, Peter J. Jones brings together 48 Ruby best practices, expert tips, and shortcuts—all supported by realistic code examples.

Jones offers practical advice for each major area of Ruby development, from modules to memory to metaprogramming. Throughout, he uncovers little-known idioms, quirks, pitfalls, and intricacies that powerfully impact code behavior and performance.

Each item contains specific, actionable, clearly organized guidelines; careful advice; detailed technical arguments; and illuminating code examples. When multiple options exist, Jones shows you how to choose the one that will work best in your situation.

Effective Ruby will help you systematically improve your code—not by blindly following rules, but by thoroughly understanding Ruby programming techniques.

Key features of this concise guide include

  • How to avoid pitfalls associated with Ruby’s sometimes surprising idiosyncrasies

  • What you should know about inheritance hierarchies to successfully use Rails (and other large frameworks)

  • How to use misunderstood methods to do amazingly useful things with collections

  • Better ways to use exceptions to improve code reliability

  • Powerful metaprogramming approaches (and techniques to avoid)

  • Practical, efficient testing solutions, including MiniTest Unit and Spec Testing

  • How to reliably manage RubyGem dependencies

  • How to make the most of Ruby’s memory management and profiling tools

  • How to improve code efficiency by understanding the Ruby interpreter’s internals

  • Table of contents

    1. Table of Contents
    2. Preface [This content is currently in development.]
    3. Acknowledgments [This content is currently in development.]
    4. About the Author [This content is currently in development.]
    5. 1. Accustoming Yourself to Ruby
      1. Item 1: Understand What Ruby Considers To Be True
      2. Item 2: Treat All Objects As If They Could Be nil
      3. Item 3: Avoid Ruby’s Cryptic Perlisms
      4. Item 4: Be Aware That Constants Are Mutable
      5. Item 5: Pay Attention to Runtime Warnings
    6. 2. Classes, Objects, and Modules
      1. Item 6: Know How Ruby Builds Inheritance Hierarchies
      2. Item 7: Be Aware of the Different Behaviors of super
      3. Item 8: Invoke super When Initializing Sub-classes
      4. Item 9: Be Alert for Ruby’s Most Vexing Parse
      5. Item 10: Prefer Struct to Hash for Structured Data
      6. Item 11: Create Namespaces by Nesting Code in Modules
      7. Item 12: Understand the Different Flavors of Equality
      8. Item 13: Implement Comparison via “<=>” and the Comparable Module
      9. Item 14: Share Private State Through Protected Methods
      10. Item 15: Prefer Class Instance Variables to Class Variables
    7. 3. Collections
      1. Item 16: Duplicate Collections Passed as Arguments Before Mutating Them
      2. Item 17: Use the Array Method to Convert nil and Scalar Objects into Arrays
      3. Item 18: Consider Set for Efficient Element Inclusion Checking
      4. Item 19: Know How to Fold Collections with reduce
      5. Item 20: Consider Using a Default Hash Value
      6. Item 21: Prefer Delegation to Inheriting from Collection Classes
    8. 4. Exceptions
      1. Item 22: Prefer Custom Exceptions to Raising Strings
      2. Item 23: Rescue the Most Specific Exception Possible
      3. Item 24: Manage Resources with Blocks and ensure
      4. Item 25: Exit ensure Clauses by Flowing off the End
      5. Item 26: Bound retry Attempts, Vary Their Frequency, and Keep an Audit Trail
      6. Item 27: Prefer throw to raise for Jumping Out of Scope
    9. 5. Metaprogramming
      1. Item 28: Familiarize Yourself with Module and Class Hooks
      2. Item 29: Invoke super from within Class Hooks
      3. Item 30: Prefer define_method to method_missing
      4. Item 31: Know the Difference Between the Variants of eval
      5. Item 32: Consider Alternatives to Monkey Patching
      6. Item 33: Invoke Modified Methods with Alias Chaining
      7. Item 34: Consider Supporting Differences in Proc Arity
      8. Item 35: Think Carefully Before Using Module Prepending
    10. 6. Testing
      1. Item 36: Familiarize Yourself with MiniTest Unit Testing
      2. Item 37: Familiarize Yourself with MiniTest Spec Testing
      3. Item 38: Simulate Determinism with Mock Objects
      4. Item 39: Strive for Effectively Tested Code
    11. 7. Tools and Libraries
      1. Item 40: Know How to Work with Ruby Documentation
      2. Item 41: Be Aware of IRB’s Advanced Features
      3. Item 42: Manage Gem Dependencies with Bundler
      4. Item 43: Specify an Upper Bound for Gem Dependencies
    12. 8. Memory Management and Performance
      1. Item 44: Familiarize Yourself with Ruby’s Garbage Collector
      2. Item 45: Create Resource Safety Nets with Finalizers
      3. Item 46: Be Aware of Ruby Profiling Tools
      4. Item 47: Avoid Object Literals in Loops
      5. Item 48: Consider Memoizing Expensive Computations
    13. Epilogue

    Product information

    • Title: Effective Ruby: 48 Specific Ways to Write Better Ruby
    • Author(s):
    • Release date: September 2014
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780133847086