Add a `k` (Kill) letter. #10

merged 2 commits into from Oct 4, 2012


None yet
2 participants

blambeau commented Oct 4, 2012

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 to debug, because happening in rspec tests. A typical use could be:

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

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

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

davejacobs commented Oct 4, 2012

This is a great patch. Thanks for following cues from existing code and for including specs.

@davejacobs davejacobs added a commit that referenced this pull request Oct 4, 2012

@davejacobs davejacobs Merge pull request #10 from blambeau/kill
add a `k` (Kill) letter.

@davejacobs davejacobs merged commit 58f2612 into davejacobs:master Oct 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment