Book Review: The Book of Ruby

Last month No Starch Press released their second Ruby book ever, The Book of Ruby by Huw Collingbourne. Their first was Ruby by Example, released four years ago in 2007! The Book of Ruby is an introductory book to the Ruby programming language.

The book covers all of the standard programming language basics: strings, classes, arrays, loops, etc. Additionally there are three chapters on more advanced subjects in Ruby: Marshal, Threads, and Dynamic Programming. Ruby on Rails is also quickly introduced in a chapter at the end of the book.

Each chapter includes a Digging Deeper section at the end that goes into more detail, warns about corner cases, or introduces an advanced usage. As someone who already knows Ruby these were the most interesting to read. They are placed inside of their own section to let someone who is just trying to learn Ruby know that they can safely be skipped.

No Starch Press is the publisher behind several fun programming books like Land of Lisp, Learn You a Haskell for Great Good, and the iconic Manga guides. When I picked up The Book of Ruby I wasn’t expecting to encounter the second coming of Why’s (Poignant) Guide to Ruby, but that’s what I was hoping for. Unfortunately, The Book of Ruby does not share any characteristics with that fun intro to programming Ruby and instead reads like an average technical book… dryly. On the positive side, I did find the author’s writing to be clear and easy to follow.

The book contains many examples but they are as contrived as possible. I understand that examples should be as simple as possible so that the reader can easily follow along but there is also a need to make the examples do interesting things to make the book more engaging.

Further, Collingbourne does not make an attempt to explain how Ruby is actually used “in the wild”. This is something that another Ruby author, Russ Olsen, does really well. Ruby has a lot of features so it is important to understand how Ruby programmers actually use Ruby. For instance, even though class variables are available, Ruby programmers try to never use them. This is something I would have liked to know when learning Ruby. However, instead of sharing these tidbits of information from his many years of Ruby experience, Collingbourne stays neutral and presents all of the information without including his opinions.

The one opinion that Collingbourne did share, is that he does not believe code style conventions are very important. This is something I can respect, and actually envy. In my own programming, I consider the time I spend to reformat my code to make it “look pretty” to be a waste. However, I still do it because it drives me crazy not to. If I could flip a switch inside my brain that would cause me to not care about code style anymore I absolutely would.

Overall, I feel like if this had been the first Ruby book I had picked up I would not have fallen in love with Ruby like I did. I am disappointed with No Starch Press for not upholding their unique style. The Book of Ruby does not differentiate itself enough to recommend over the other Ruby titles that are out there.

Upgrading to RTorrent 0.8.5

Recently I went through the process of upgrading rtorrent-0.8.2 and libtorrent-0.12.2 on my Ubuntu 9.04 box to the latest versions, rtorrent-0.8.5 and libtorrent-0.12.5. Unfortunately what should be a simple process was rather complicated because the developer has not yet updated the project’s website to reflect all of the changes that have been made to the configuration options in version 0.8.4 (which was released nearly a year ago!). After dozens of Google searches I was able track down the information I needed. In addition, I have added notes on hiccups I ran into when compiling RTorrent from source and getting RTorrent to work with dtach.

Continue reading

Rails Rumble ’09: Grocery Tracker

Update: The Grocery Tracker website is no longer online.

This year I once again participated in the RailsRumble competition with the goal to build a Rails app in 48 hours. My amazing teammates Arya Asemanfar, Gary Tsang, and Alex Le and I worked together tirelessly to build an application for tracking grocery purchases. The result is Grocery Tracker.

Grocery Tracker

grocery tracker landing page screenshot

The objective of Grocery Tracker is to make it really easy to visualize how much you are spending on groceries and how your buying habits are changing. For me, it is interesting to see what percent of my grocery purchases are going towards “Snacks & Candy” as well as my historical spending in that category. Grocery Tracker allows me to quickly answer questions like “Am I spending less on snacks now than I was 3 months ago?”.

Continue reading

Capturing Output from PUTS in Ruby

When writing unit tests for my simplesem interpreter, one test in particular was problematic. In simplesem, the set write instruction prints output to the screen.

// place Hello World! on the 'write' buffer
set write, "Hello World!"

Internally, the interpreter is just passing the second parameter, “Hello World!”, to the puts method in Ruby. This makes it difficult to use traditional test/unit assertions to check that the simplesem instruction is working.

I eventually found two solutions for this. The first, suggested by David Stevenson at Pivotal Labs, is to use mocha to check that puts was called on the object.

require 'test/unit'
require 'rubygems'
require 'mocha'
 
class SimpleSemParserTest < Test::Unit::TestCase
 
  def test_set_stmt_write
    parser = SimpleSemParser.new
    parser.expects(:puts).with("Hello World!")
    parser.parse('set write, "Hello World!"').execute
  end
 
end

This solution is fine for most situations; Mocha will throw an exception if puts is not called. However in my case, it was unsuitable because puts was not being called on the SimpleSemParser object but instead on a Treetop syntax node that I did not easily have access to within the unit test.

I knew that if I could capture the output from the puts method into a variable I would be able write the test using a standard assert_equal. After some googling I discovered that this functionality is built into the ZenTest gem. After rewriting the test looked like this.

require 'test/zentest_assertions'
 
class SimpleSemParserTest < Test::Unit::TestCase
 
  def test_set_stmt_write
    out, err = util_capture do 
      parser = SimpleSemParser.new
      parser.parse('set write, "Hello World!"').execute
    end
 
    assert_equal "Hello World!\n", out.string
  end
 
end

This works great. However, should we decided that we do not want to use an external gem, with a little effort we can bypass ZenTest and implement util_capture ourselves.

Continue reading

Automate Updating Your Git Repositories

I have a half dozen TextMate bundles that I am grabbing the latest versions from GitHub instead of from the main TextMate subversion repository. For a long time I have been updating each of the bundles’ Git repositories by cd’ing into each directory and running git pull. Doing this over and over became silly and I was annoyed from doing all that typing.

Today, I finally wrote a basic bash script that automates updating each of the repositories. Sometimes it is surprising how simple an automated solution turns out to be.