Are your Ruby methods timid? Do they constantly second-guess themselves, checking for nil values, errors, and unexpected input? With Confident Ruby, you’ll learn patterns, idioms, and language features that help to write code in a clear, straightforward style. Your code will be more testable, easier to read, and easier to debug. And you’ll be a happier programmer!

Foreword by Sandi Metz, author of Practical Object-Oriented Design in Ruby.

This book was written and produced entirely by the author. We are proud to be distributing it.

Buy Now

Select a DRM-free Format:

In Stock
Buy the eBook and get these DRM-free formats delivered immediately:
  • epub (for iPhone/iPad, Android, eReaders)
  • mobi (for Kindle)
  • PDF
We can automatically send them to your Kindle, and your Dropbox. (You'll need to log in to enable these options.)
 

About this Book

  • 296 pages
  • Published:
  • Release: P1.0 (2014-04-14)
  • ISBN: pending
32 Patterns for Joyful Coding

For many programmers, discovering Ruby is a revelation. They are overjoyed by how elegantly and succinctly they can state problems in the language. But then the real world creeps in, and that joy gradually wanes as the code becomes cluttered with distracting edge case scenarios, error handling, and checks for nil.

Confident Ruby is a guide to recapturing the joy of writing Ruby by employing small patterns and stylistic choices to make each method tell a coherent story. Over the course of 32 patterns, you’ll learn how to isolate uncertainty at the borders of your code; how to avoid repetitive conditionals; and how to liberate your code from the chore of nil-checking. You’ll discover the four parts of a method, and how to use those parts to construct a narrative. Following along with real-world refactoring sessions, you’ll learn how to apply these lessons to legacy code.

Whether you are a novice or an experienced Ruby hacker, this book will help you become a more confident and joyful programmer.

What You Need

  • A working Ruby installation
  • A text editor

Contents and Extracts

  • Foreword
  • Preface
  • Introduction
  • Performing Work
    • Sending a strong message
  • Collecting Input
    • Introduction to collecting input
    • Use built-in conversion protocols
    • Conditionally call conversion methods
    • Define your own conversion protocols
    • Define conversions to user-defined types
    • Use built-in conversion functions
    • Use the Array() conversion function to array-ify inputs
    • Define conversion functions
    • Replace “string typing” with classes
    • Wrap collaborators in Adapters
    • Use transparent adapters to gradually introduce abstraction
    • Reject unworkable values with preconditions
    • Use #fetch to assert the presence of Hash keys
    • Use #fetch for defaults
    • Document assumptions with assertions
    • Handle special cases with a Guard Clause
    • Represent special cases as objects
    • Represent do-nothing cases as null objects
    • Substitute a benign value for nil
    • Use symbols as placeholder objects
    • Bundle arguments into parameter objects
    • Yield a parameter builder object
    • Receive policies instead of data
  • Delivering Results
    • Write total functions
    • Call back instead of returning
    • Represent failure with a benign value
    • Represent failure with a special case object
    • Return a status object
    • Yield a status object
    • Signal early termination with throw
  • Handling Failure
    • Prefer top-level rescue clause
    • Use checked methods for risky operations
    • Use bouncer methods
  • Refactoring for Confidence
    • MetricFu
    • Stringer
  • Parting Words
  • Colophon

About the Author

Avdi Grimm is the author of Exceptional Ruby, head chef of the RubyTapas screencast series, and a co-host of the Ruby Rogues podcast. He lives in Pennsylvania with his wife and a steadily incrementing number of children.

Latest Topics in the Forums…

Join the Discussion