Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a k (Kill) letter. #10

Merged
merged 2 commits into from Oct 4, 2012
Merged

Add a k (Kill) letter. #10

merged 2 commits into from Oct 4, 2012

Conversation

blambeau
Copy link
Contributor

@blambeau 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
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

@davejacobs
Copy link
Owner

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

davejacobs added a commit that referenced this pull request Oct 4, 2012
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants