Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
4 October 2006 @ 1pm

Tagged
Books, Object Oriented, Rails, Ruby, Software

Discuss

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:

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.

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.


9 Comments

Posted by
Matthew Bowen
4 October 2006 @ 3pm

David’s book is the jam. IMO, chapter 13, on dynamic Ruby, really shows where the language shines — I’ve re-read it a couple times now.

- m@


Posted by
Rob
4 October 2006 @ 4pm

Matthew: Agreed, I’ve found that chapter, and the regex chapter, have helped me a lot even upon re-readings.


Posted by
khang toh
4 October 2006 @ 8pm

Hi

Nice review on the book, it was one of the better ones that I read for Ruby/Rails books.
It will be nice f you can add an Gizmoojo Adgent widget to the post and get some rewards for posting the review.

Gizmoojo Adgent is offered as a widget at Widgetbox. Please check it out.

http://www.widgetbox.com/widget/gizmoojo?category=money


Posted by
Juan Lupión
6 October 2006 @ 1am

AWDwRoR is the book for Rails newcomers, but what next? IMO this is the next book to read, as you cannot truly understand RoR without a thorough knowledge of Ruby’s dynamic innards.


Posted by
Review of "Ruby For Rails" by David A. Black
6 October 2006 @ 7pm

[…] Rob Sanheim (one of the Ajaxians) has put together a great review of Ruby For Rails by David Black. It’s a great book, and if you haven’t got it yet, let Rob’s review convince you. I personally found it an interesting read and it’s really easy to get stuck into. It’s an ideal book if you’ve made it through Agile Web Development for Ruby on Rails and are hankering for more as it dives into the Ruby side of things a lot more. […]


Posted by
Peter Szinek
7 October 2006 @ 7am

I am happy to see that more and more good reviews are coming out on this wonderful book - I hope the Ruby community will embrace it with the same enthusiasm as the Pickaxe earlier, since this book would be really worth it.

I have been also wondering about the Pickaxe vs. R4R question… I have summarized my thoughts here:

http://www.rubyrailways.com/the-sadly-neglected-pickaxe-killer/


Posted by
Gary Henderson
7 October 2006 @ 9pm

Great book, and a superb next step after Agile Web Dev with Rails. Perhaps the best technical writing I’ve ever seen. Many new insights for me. Thanks for the review.


[…] Rob Sanheim has a great review of Ruby for Rails on his blog. I personally love this book. It was a great follow-up to Agile. Just the right amount of hand holding and hard, cold information. I still find myself using it for reference from time to time. […]


Posted by
Tony Spencer
9 October 2006 @ 9pm

Great review of a great book. Also David is now offering ruby on rails training for both beginners and those looking for an advanced course:

http://www.raleighonrails.com/


Leave a Comment

GoF Patterns: Helping Losers Lose Less Continous Builder gets some Campfire love