如何在没有 catch/try/raise 的情况下展开(多级返回)堆栈?
当 catch
/try
不可用时(即我要展开的代码不受我的控制),我想将堆栈展开到任意级别。这可能吗?
例如,在测试中,我想让我的测试调用一个检查先决条件的方法,如果不满足这些先决条件,则展开到测试的调用者(在 caller[1]
处恢复) )。由于测试工具不受我的控制,因此我无法在适当的位置设置 catch
块。
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 prereqs 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可以。多级返回的唯一其他机制是
callcc
,但这也需要在您无法控制的级别上创建延续。也就是说,在 Ruby 中,没有什么是真正超出您控制的:您可以为测试框架中的方法命名并添加您想要的任何代码。
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.