Refactoring in Ruby

I’ve started working my way through Refactoring in Ruby, by William C. Wake and Kevin Rutherford. I haven’t been this excited about a book since Michael Feathers’ Working Effectively with Legacy Code. I feel like a schoolboy, relearning refactoring as if for the first time.


The core concept of the book was not clear from the title, so I didn’t realize quite what I was holding until I started to read it. Refactoring in Ruby is neither a catalog of refactorings nor an introduction to refactoring. Rather, it’s a workbook intended to accompany Refactoring: Ruby Edition.

I’m only a short way into the book, but it’s clear that in order to get the most out of it, you need to follow a few rules:

  1. Order a copy of Refactoring: Ruby Edition if you don’t have one already. RiR and that book are essentially companion volumes. RiR makes extensive references to Refactoring.
  2. Work the exercises. This is a workbook; how much you get out of it depends in a large part on how much you put into it.
  3. Follow the steps carefully. I had started into the first code exercise, and the text instructed me to use the Extract Method refactoring on a method. “Extract method? I know how to do that” I thought to myself. Then I stopped, and forced myself to look up “Extract Method” in Refactoring: Ruby Edition. Turns out there are five (five!) steps to doing Extract Method right. If you follow the steps as written, you can stop after any one of the steps and still have fully functional code with all tests passing.

I’m realizing as I start to work through this book that, much as I love a good refactoring, my approach to refactoring has been pretty cavalier up until now. I feel like I have a new bar to hold myself to as far as refactoring in a controlled, incremental, disciplined manner. I feel like a n00b again, and it feels good.

If anyone else is working their way through this book and might be interested, I’m publishing my notes and exercise solutions on Github.

3 comments

  1. Hi Avdi, Glad you're enjoying the book — seems like you've picked up exactly what we hoped: that doing the exercises is the key! And great idea to share your working. Cheers, Kevin

Leave a Reply

Your email address will not be published. Required fields are marked *