Ylan Segal

Book Review: Practical Object-Oriented Design in Ruby - Sandi Metz

If you call yourself a ruby programmer, you should read this book.

Sandi Metz has done a great job explaining how to write maintainable applications, in a way that is accessible to programmers at any skill level. Her book is concise (less than 250 pages, including the index), but jam-packed with great nuggets of practical advice and coding techniques that you can start applying immediately in your projects. Her style is easy to read, with many code examples that show you the evolution of code, as an application changes and is refactored.

Among the techniques discussed:

  • Use dependency injection to avoid coupling between objects. (Makes them easier to test too)
  • To write great APIs, focus on the messages being sent between objects. (She shows why you would actually want to use a UML diagram)
  • By creating role tests and applying them to test doubles, you can avoid the ‘leaving the dream’ problem, where stubs in tests obscure the fact that objects interfaces have changed.

Links: Publisher Site, Amazon.com

StandardError Casts Message to String in Ruby 1.9

When passing a message to StandardError in ruby 1.8, it will keep that object intact. Ruby 1.9 will convert to a String. Spent more time than I wanted to chasing this down.

In ruby 1.8.7:

1
2
3
4
5
6
>> RUBY_VERSION
=> "1.8.7"
>> err = StandardError.new(Object.new)
=> #<StandardError: #<StandardError:0x78952527>>
>> err.message.class
=> Object

In ruby 1.9.3:

1
2
3
4
5
6
>> RUBY_VERSION
=> "1.9.3"
>> err = StandardError.new(Object.new)
=> #<StandardError: #<Object:0x456c5f50>>
>> err.message.class
=> String

Run Changed Specs

I usually create a branch while working on a feature or bug. I found it helpful to add the following script to my path:

1
2
3
#! /bin/bash

git diff --name-only master..HEAD | grep 'spec.rb' | xargs rspec

It’s quick and dirty, but effective. It asks git for a list of filenames that have changed in the current branch, filters them to find specs and runs them with rspec.

Faster Rspec: Jruby, Spork and Nailgun

UPDATE: You can use bundler binstubs to squeeze a bit more performance


Much has been said about Rails slow start-up time on large projects. It is especially painful when trying to do TDD and each test takes 30 seconds to run, mainly in startup time.

As a consequence, there have been many attempts to pre-load the Rails environment and have it ready to test. I tested some options and saved 25 seconds on each test run.