15 November 2013

Book Review: Rebuilding Rails

Rebuilding Rails from Noah Gibbs, a self-published eBook, is a guided tour through the internals of Ruby on Rails.  His basic premise is that the best way to get to know a combustion engine is to build one yourself!

Ruby on Rulers

The goal is to pull back the veil and demystify some of the "magic" behind Rails.  We do this by building a web application framework similar to Rails, named "Ruby on Rulers".  By the end of the book, Rulers contains the most fundamental features found in Rails.

As he walks us through the development of Rulers, he shows us how Rails is "merely" a MVC Rack application that takes advantage of the elegant dynamic features of the Ruby language.

In each chapter, Gibbs tackles a core aspect of Rails:

  • the core request/response processing chain
  • how requests are routed to the correct controller
  • how views are rendered
  • how models front for a data source (including discovering attributes and dynamic finders)
In each component, Gibbs leads us through a first-iteration implementation. Of course Ruby on Rails is a far more mature framework with a more sophisticated implementation.  Keeping it simple makes understanding the core of what's going on in Rails accessible.

Sample Source

Chapters begin by motivating the need for the targeted feature.  He then points you to his Github repo that contains sample code, chapter-by-chapter.

In Rails

At the end of each chapter, Gibbs points back to the parts of Ruby on Rails that implement the chapter's feature.  Included are exercises that challenge the reader to go a level deeper in their understanding.

Highlights

A few highlights:
  • Ruby on Rails is a "Rack Application."  Basically, this means that it provides a proc that takes a single argument (a hash named "env" containing both application environment parameters and request-specific parameters) and returns a three-element array: [HTTP return value, HTTP header parameters, the body of the response].  That's it.
  • Routes are "merely" method calls on a Routing object that creates one or more mappings from a regular expression to a controller-and-action pair.  The routing config is Ruby code evaluated directly (in a instance_eval() call).
  • Rails leverages the explicit separation of "message" from "method" in Ruby to dynamically create accessors for attributes on Models.  This simply involves implementing the "method_missing" method on the SQLModel base class which uses schema data to load (and update) attributes that match the name of columns in the table.
  • Providing "context" to ERb templates is a built-in feature of that template library. Controllers provide instance variables to Views by simply enumerating instance variables of the Controller (self.instance_variables.each {...}) and adding them to the template context (which is simply a hash).

The Good and The Bad

Overall, this is a really great resource.  There honestly is no better way to get a deeper understanding of something like Rails than to face down the problems that the authors of that framework did.  Gibbs clearly understands the material and is enthusiastic.

The scope of the book is just right.  Gibbs does a good job of going wide and shallow and not getting stuck in one particular area.  It's an excellent survey.

The book is self-published and it shows.  There are loads of grammar errors.  Much of the narrative is awkward.  In places, Noah attempts to be folksy or conversational in tone and it often falls a bit flat.  Talking people thought the innards of a web framework is not easy.  But this is where editors earn their pay.

In the final chapter, there is actually a missing chunk of code.  The final version of the Rulers::Application class (as it appears in the book, itself) is missing the #call(env) method.  For the engaged reader, this represents an unexpected exercise, but it's a little frustrating.

Throughout the book, the code examples includes some oddly styled code.  Gibbs makes liberal use of single character variables.  This makes the code much more difficult to grok.  Better to have used a smaller font and more verbose identifiers.

From a development approach perspective, I'm disappointed that there were no tests.  In building up something like a framework, having unit tests are not only helpful in showing where the interfaces lie, but also provide a basis for refactoring and adding new features with confidence.  There were some significant chunks of code that one is expected to key in without error.  Gibbs never promised to include tests, it's just something I felt like was really missing.

The exercises at the end of each chapter are really fantastic.  They are challenging!  And in being this way, it really helps the reader deepen their understanding.  You won't be able to just gloss your way over them.  He explains what you should do, but only hints at how it should be accomplished.  

Conclusion

All-in-all, Gibbs delivers.  This is a really great book; truly a labor of love.  $39.99 is a little steep given the lack of fit-and-finish.  That said, I'm really glad that I went through this book and grateful for the deeper understanding of how Rails works, under the covers.