Panasonic Youth

Ruby For Rails by David A Black - Book Review

Twenty Second Summary: A fantastic guide to Ruby for programmers of all skill levels - definitely not just for Rails newbies.

You might imagine that David Black’s Ruby for Rails is a quick run through Ruby as an attempt to cash in on Rails hype. While I’m sure the interest in Rails helped in getting this book published, this is not a quick cash in attempt. This is one of the best Ruby books available. Dare I say this book tops the esteemed PickAxe (ie Programming Ruby: Second Edition) by the Pragmatics? Maybe…

David Black is a director of Ruby Central, ruby-talk regular, consultant and presenter. He knows and loves Ruby, and it shows in this book. His style is informal and easy to read. My only (minor) complaint is that its a bit verbose at spots, with some points being made many times over in different chapters.

The book is divided into four parts. Part one is the standard “quick-start” piece, with three chapters doing a quick overview of Ruby and Rails. He also introduces the Rails app that is used for examples throughout the book, R4RMusic - a classical sheet-music store. One minor nitpick is that Black uses straight sql instead of migrations for his database setup.

The second part cover’s the fundamentals of Ruby, so if you’ve read the PickAxe or one of the online intros much of this will be familiar. He covers modules and classes, control flow, exceptions, and variables and methods. Experience programmers from other languages can probably skim through this pretty quickly, but those newer to programming will find this material invaluable. There is also a chapter devoted to the all important role of self and how scope works in Ruby. This helped clear up some confusion I had about just where methods go when they are declared at the top level, and where and where “functions” like puts and friends come from in irb.

Part three covers the core libraries, regular expressions, and metaprogramming. String, Numeric, Time, Date, Array and Hash are discussed in depth, with plenty of examples, which is always helpful if spent any time trying to figure things out with the online api. The regex chapter is one I continue to return to, as I can never remember the syntax needed to get typical things done.

Chapter 13 is the metaprogramming chapter, titled “Ruby Dynamics”, and my favorite out of the whole book. Black first covers the oft-misunderstood singleton class, how to access it and modify it, and where it lies in the message chain. Class methods are explained as a special case of singleton methods on objects of class Class, using some helpful diagrams to show how inheritance works with all of this. The eval family of methods is then covered, starting first with the redheaded stepchild “eval”, followed by little brother instance_eval, finally covering everyone’s favorite girl next door: module_eval, aka class_eval. If you’ve done any digging in the Rails source or done metaprogramming yourself, you’ve probably seen or used it. Its one of the most commonly used methods for adding or changing methods at runtime in Ruby.

Procs and lambdas are explained well along with their relationship with blocks. This section includes a nice analogy for closures: <blockquote>Creating a closure is like packing a suitcase. Wherever you open the suitcase, it contains what you put in it when you packed it. When you open a closure (by calling it), it contains what you put into it when it was created.</blockquote>

Callbacks such as method_missing, included, and inherited are covered, with an illustration from ActiveRecord::Base.inherited showing how new AR subclasses tracked using the callback. There is an all too brief section covering open classes and how to change existing methods, and the dangers that come with that power. The one problem I had with the entire dynamics chapter is that I wish there was more of it! The content is spot-on, but there could be a whole separate chapter discussing the pros and cons of the various methods available and idiomatic metaprogramming versus inadvisable techniques.

Part four is the final and most Rails-heavy section. He walks through building up a rich domain model through adding on to the ActiveRecord models and also how to separate functionality in the view with helpers and partials. Much of this content was not new to me, having already covered much of it with the Agile book, but I can see the value for newer Rails programmers. My second fave chapter, 17, explains techniques for exploring the Rails source. The ActiveRecord class method “belongs_to” is used for the primary example, but again I was frustrated as it seemed Black only scratched the surface of what is going underneath. A whole chapter could be done just showing how a belongs_to association is generated and handled, and could use the Rails source to illustrate some of the common metaprogramming techniques that you see in Rails and elsewhere. As it is the coverage of belongs_to goes far enough to show how the methods goes from an instance method in ActiveRecord::Associations::ClassMethods to a class method available to ActiveRecord::Base. Whiles its enlightening, I wanted more from this chapter.

Overall, Ruby for Rails is an excellent book. My main complaint has to do with wanting more advanced, in depth material, and given the amount of ground the book does cover (just under 500 pages, btw) I understand the book had to be completed in a reasonable amount of time. The material that is covered is explained clearly and patiently, with a lot of coverage given to the core classes and object model that make up Ruby. For Java/.Net refugees or those Ruby beginners looking to enhance their skill with Ruby, I would strongly recommend this book. And yes, I’d even recommend it over the PickAxe.