From the crack team (Micah Gates and myself) that brought you job_interview, I’m proud to announce the availability of close_enough, a gem that will save you from all of those niggling NoMethodErrors that occur when you mis-type a method name.

The concept is very simple, and based on an algorithm used in most spell checkers and autocorrect systems currently in use. Calculating the Damerau-Levenshtein distance between two words can reliably (within a certain margin of error) indicate whether a known word was inteded when an unknown term was encountered in user input.

In Ruby, we have a wonderful metaprogramming facility in method_missing. What we’ve done in close_enough is to monkey-patch method_missing on Object to calculate the Demerau-Levenshtein distance between existing methods and the non-existent called method, and invoke the nearest one if it’s “close enough” (close enough currently being an edit distance of < 3).

So how does it work?

It’s pretty straightforward. Let’s say you’re speed typing, maybe on an unfamiliar keyboard (I use a Sun Type 5 at home, but usually Dell QuietKeys at work… very different layout and key responsiveness). Sure, typos are going to happen, and it can be annoying and slow you down. But with close_enough…

Close Enough Examples
require 'close_enough'
obj =
=> Object
str = "foo"
=> 3
=> "oof"
ary = [1,2,3,4,5]
ary.mpa {|a| a + 2}
=> [3,4,5,6,7]
=> 15

It’s that easy. Quick, inaccurate typing becomes a non-issue. Or, at least, a minor issue. Okay, really, you should never use this in any kind of production environment.

It’s available from Rubygems, and the source is on Github.