View Review Details


Book:   Ruby Cookbook
Subject:   Ruby Cookbook Review
Date:   2006-12-21 12:51:04
From:   Vince W.
Rating:  StarStarStarStarStar

This review was originally published for RubyDC on Urbanpuddle.com:


Life is short, proclaims the authors of O'Reilly's Ruby Cookbook. You have real problems and this book is here to solve them, they go on. Weighing in at around 850 pages, there certainly is a chance that what ever problem you have could be addressed in this book. But is it all cartoon foxes or a tale with descriptions and plot twists worthy of reading in your bathrobe over a cup of chamomile? Thankfully no. In this book, O'Reilly delivers a densely packed tome filled with information, most of it unrelated, intended to solving real problems.


If you've never used a recipe book before, the premise is simple. They teach you how to put together code to create a particular effect or output. Reading tons of code on paper can be awfully boring but thankfully Ruby lends itself well to these types of books largely because it's a human readable language and packs a lot of functionality in very little space. For instance, you can expect:
<% 3 times.do %> yeah


Will output yeah yeah yeah as you would expect when reading
three times, do 'yeah'. In the Ruby Cookbook, authors Lucas Carlson and Leonard Richardson have divided the problems into these logical groups:


1. Strings
2. Numbers
3. Date & Time
4. Arrays
5. Hashes
6. Files & Directories
7. Code Blocks & Iterations
8. Objects & Classes
9. Modules & Namespaces
10. Reflections & Metaprogramming
11. XML & HTML
12. Graphics and Other File Formats
13. Databases & Persistence
14. Internet Services
15. Web Development: Ruby on Rails
16. Web Services and Distributed Programming
17. Testing, Debugging, Optimizing, and Documenting
18. Packaging and Distributing Software
19. Automating Tasks with Rake
20. Multitasking and Multithreading
21. User Interface
22. Extending Ruby with Other Languages
23. System Administration


As you can see there is a lot of information in this book. <tangent>As I was flipping through it I began to wonder how I could possibly make room in my brain for all this knowledge. If I absorbed it all I'd probably have to discard some pretty fundamental things like remembering to zip up my fly. Trust me, nobody wants that. My guess is that Mssrs. Carlson & Richardson are in similar predicaments and my thanks to go them and their Significant Others for potentially sacrificing valuable basic skills in order to bring you all this wonderful code. </tangent>


Each recipe in the book is divided into three sections: Problem, Solution, and Discussion. As you can infer, the problem gives a concise description of the issue that the Solution will then resolve. The Solution portion is where most of the code is and, like all O'Reilly books, they do a good job of separating the text from code so there's never any confusion. The Discussion is often the longest part of the recipe and contains not only details on the solution but the occasional alternative approach as well.


I can't say that each recipes contained in this book is staggeringly useful. For instance, I personally do not foresee ever needing 2.14: Doing Math with Roman Numbers. But I guess some would-be gladiators out there might find that helpful. But the recipes that I did find interesting were very valuable and made the price of the book well worthwhile. I particularly enjoyed the recipes on Classifying Text with a Bayesian Analyzer, Documenting Your Website, and Checking a Credit Card Checksum.


Other recipes, while not urgently required, can add a polish and professionalism to your code to set you apart from your peers. For instance there is a very simple recipe for wrapping text which you can use as an alternative to truncating:


def wrap(s, width=78)
s.gsub(/(.{1,#{width}})(\s+|Z)/, "\\1\n")
end


Using it like: puts wrap("This text is not too short to be wrapped.", 20)


I did find some solutions a little lacking. The Sending Mails with Rails recipe is different enough from the similar recipe in Chad Fowler's Rails Recipes book to make it useful. Neither solution independently helped me craft a newsletter function for my website but both combined gave me the knowledge I needed to write my code the way I needed it. Likewise, Generating PDF Files recommended using PDF-Writer which I found slow for the amount of fields (> 200) and records (> 500) that I need for my application. Based on a recommendation from a fellow RubyDC member, I will be checking out Ruport and RTex to see if it helps. Honestly, it could be that all solutions give me the same headaches.


Overall I was really impressed with the book. As with any recipes book, the utility is directly proportional to your need. Ruby Cookbook is no different there. It makes sense that, since each program is unique, some proposed solutions won't fit perfectly. Yet they will still otherwise give you a great launching point towards finding your own answer! Most importantly, as the types of applications you need to code change and as their complexity grow, you will find methods to avoid many of the usual speedbumps along the way within these pages.


See larger cover

"This sort of book lives and dies by two criteria - the quality of the code and the usefulness of the recipe selection. Ruby Cookbook wins on both. "
--Tony Williams, Honestpuck.com