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
- About This eBook
- Title Page
- Copyright Page
- Dedication Page
- Contents
- Foreword
- Preface
- Acknowledgments
- About the Author
- 1. Accustoming Yourself to Ruby
-
2. Classes, Objects, and Modules
- Item 6: Know How Ruby Builds Inheritance Hierarchies
- Item 7: Be Aware of the Different Behaviors of super
- Item 8: Invoke super When Initializing Subclasses
- Item 9: Be Alert for Ruby’s Most Vexing Parse
- Item 10: Prefer Struct to Hash for Structured Data
- Item 11: Create Namespaces by Nesting Code in Modules
- Item 12: Understand the Different Flavors of Equality
- Item 13: Implement Comparison via “<=>” and the Comparable Module
- Item 14: Share Private State through Protected Methods
- Item 15: Prefer Class Instance Variables to Class Variables
-
3. Collections
- Item 16: Duplicate Collections Passed as Arguments before Mutating Them
- Item 17: Use the Array Method to Convert nil and Scalar Objects into Arrays
- Item 18: Consider Set for Efficient Element Inclusion Checking
- Item 19: Know How to Fold Collections with reduce
- Item 20: Consider Using a Default Hash Value
- Item 21: Prefer Delegation to Inheriting from Collection Classes
-
4. Exceptions
- Item 22: Prefer Custom Exceptions to Raising Strings
- Item 23: Rescue the Most Specific Exception Possible
- Item 24: Manage Resources with Blocks and ensure
- Item 25: Exit ensure Clauses by Flowing Off the End
- Item 26: Bound retry Attempts, Vary Their Frequency, and Keep an Audit Trail
- Item 27: Prefer throw to raise for Jumping Out of Scope
-
5. Metaprogramming
- Item 28: Familiarize Yourself with Module and Class Hooks
- Item 29: Invoke super from within Class Hooks
- Item 30: Prefer define_method to method_missing
- Item 31: Know the Difference between the Variants of eval
- Item 32: Consider Alternatives to Monkey Patching
- Item 33: Invoke Modified Methods with Alias Chaining
- Item 34: Consider Supporting Differences in Proc Arity
- Item 35: Think Carefully Before Using Module Prepending
- 6. Testing
- 7. Tools and Libraries
- 8. Memory Management and Performance
- Epilogue
- Index
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
You might also like
video
Effective Ruby
4+ Hours of Video Training Overview Effective Ruby LiveLessons Video Training explains the tips, tricks, and …
video
Learn Ruby the Hard Way
The media elements for Learn Ruby the Hard Way demonstrate key points in the text.
book
Clean Ruby: A Guide to Crafting Better Code for Rubyists
Learn how to make better decisions and write cleaner Ruby code. This book shows you how …
book
The Ruby Workshop
Cut through the noise and get real results with a step-by-step approach to learning Ruby programming. …