开发者

How to unwind (multi-level return) the stack without catch/try/raise?

开发者 https://www.devze.com 2023-02-10 09:45 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号