How to unwind (multi-level return) the stack without catch/try/raise?
I would like to unwind the stack to an arbitrary level when catch
/try
is not available (i.e., the code to which I'm unwinding is out of my control). Is this possible?
For example, in testing, I would like to have my tests call a method that checks for prerequisites, and unwinds to the caller of the test if those prereq开发者_运维问答s aren't met (resume at caller[1]
). Since the test harness is out of my control, I can't set up a catch
block at the appropriate place.
No. The only other mechanism for multi-level returns is callcc
, but that too requires creating the continuation at a level that's out of your control.
That said, in Ruby nothing is really out of your control: you could alias the method in the test framework and add any code you'd like.
精彩评论