davejacobs/letters — pull request #10 by blambeau (2012-10-04 20:50:22+00:00)
Title: Add a `k` (Kill) letter.

Somewhat similar to #4 (q for Quit), but this one accepts a `max` that specifies a maximal number of calls before raising a KillError.

I wrote this one this morning while debugging an infinite recursion (SystemStackTrace), which proved difficult, because happening in rspec tests. A typical use could be:

def visit(*)
k(max: 3)
... visit(*) ... # unbounded recursion
end

The KillError naturally shows the stacktrace of the last 3 calls, which proved very nice for fixing my stuff this morning. In my case, since the `visit` method was not called on the same instance, I used the following trick:

def visit(*)
self.class.k(max: 3)
... visit(*) ... # unbounded recursion
end

I don't know whether this has to be merged. It is basically a shortcut for something like:

def visit(*)
a{ @max_calls ||= 0; @max_calls += 1; @max_calls <= 3 }
... visit(*) ... # unbounded recursion
end