下面是我的示例代码:
def test(v)
test(v-1) if v > 0
p v
end
如果我调用 test(11893)
它工作正常。
如果我有 v > 11893
,它会抛出 SystemStackError
。
如何增加此错误的限制?
最佳答案
MRI 有 tail recursion优化默认关闭。但是一个人可能 turn it on :
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
此外,代码本身必须使用尾递归:
def test(v)
return unless v > 0
p v
test(v-1)
end
关于 ruby : stack level too deep (SystemStackError) maxlimit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714780/