Practical Ruby Projects
Practical Ruby Projects is a pretty poorly named title, but, luckily, that doesn't stop it from being a very strong book. The book actually turns out to be an exploring-the-Ruby-programming-language-by-example book. These aren't your trivial beginners-only tasks though. There's enough meat in these pages for the intermediate crowd to really get into.
Let me start by clarifying my earlier comment about the title. It's clear this book is named after the series it appears in, instead of the actual content it holds. There are lots of projects in the book and they are definitely written in Ruby, but Practical is not the word I would use to describe them. Fun, on the other hand, would be a great word. Beyond that, the code and concepts used in these projects is well worth studying. Just don't expect to find the typical (for Ruby) collection of Web programming tips inside. To me, that was a big plus. The title just misrepresents what's inside.
The projects you will find in the book include: MIDI music generation, SVG graphic building, pocket change simulations, a turn-based strategy game, a Mac OS X GUI, a genetic algorithms framework, as well as both a parser and interpreter for the Lisp programming language. While these projects obviously tackle subsets of each problem space, they go deep enough to serve as a solid introduction in each area. The author is also good at focusing on the more interesting aspects of each challenge and throwing in a few twists to keep your interests high.
Let me give you an overview of the MIDI project so you will know I mean. This chapter explains some basics of MIDI protocol, moves on to showing how to interface with native MIDI libraries on Linux, Windows, and Mac OS X, adds a look at how to manage timed events with Ruby, then closes with a detour into the live coding of music algorithms. The middle tasks, building native interfaces and controlling timed events, are the meat and most of the chapter's time is spent there. The former shows how to use C code from Ruby without writing C and how to manage what code is used on each platform. With the latter, you get very practical examples of how to schedule and later invoke callbacks. The live coding section may sound like an odd include, and I guess it is, but it does give the author an opportunity to show ways to make changes to a continuously running Ruby program.
Now if that particular topic doesn't strike your interests, its very likely one of the others will. Those of us who like animation will enjoy the SVG section, the gamers will probably take the simple strategy game and run with it, the algorithm buffs will enjoy the coin simulations which dip into dynamic programming and genetic algorithms, and for the real geeks among us there are two chapters on Lisp. If more than one of those categories fits you, you're really going to enjoy this book. I'm pretty sure there's something for everyone in here.
If I had to single out the least interesting project in the book, I would say it's the Mac OS X GUI. I'm a Mac guy and I'm interested in the RubyCocoa system this chapter covered, but the presentation fell down a bit here. Due to space constraints, the author chose to deliver this content in a very non-standard way. Unfortunately, that means you are mostly just copying code in this section without really gaining a deeper understanding of what you are doing. It also means this section's code won't really stick with you as you move on to the proper ways to build a Mac GUI. It was the only project I found disappointing.
What really makes this title is the code. It's top notch in almost all cases. The author really dives deep into the Ruby pond and makes sure we see a little bit of everything. There's coverage of standard Rubyisms like the iterators and the standard unit testing library, but you'll also catch some lesser shown tricks like programmatically building dynamic subclasses. Every section has some interesting code in one or more places. More than once I found myself asking, "Now why did he write it that way?," only to have it answered in the following explanations. I can't name a better way to learn intermediate Ruby.
There was one exception to the quality code, though it's a minor complaint. The author does misuse Ruby's open classes a few times. There are multiple cases where he adds code to
Enumerable that's dependent on methods in
Array. He seems to have trouble knowing when to separate the two. Don't pick up that bad habit.
All in all, if you prefer to learn by example over theory, this is easily the best book for that with Ruby. The projects are inspiring and I found myself still fiddling with the code after finishing a section more than once. While playing around you will be exposed to some great code and fresh ideas. It's hard to beat that.