我正在遵循答案 here 的建议,并确认 :tailcall_optimization=>true 和 :trace_instruction=>false,但我仍然得到:
SystemStackError: stack level too deep
...
... 8696 levels...
我做错了什么?
def persist_shipments_then_next(prev_data)
persist_shipments(prev_data)
next_token = prev_data['NextToken']
puts next_token
unless next_token.nil?
next_data = @client.list_inbound_shipments_by_next_token(
next_token
).parse
persist_shipments_then_next(next_data)
end
end
更新:我从 application.rb 中删除了以下内容,它有效吗?:
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
最佳答案
您需要为递归函数调用设置某种终止条件。您已将该条件设为 unless next_token.nil?
,但您确定 next_token
在您尝试终止时变为 nil,可能您只需要执行 除非 next_token
def persist_shipments_then_next(prev_data)
persist_shipments(prev_data)
next_token = prev_data['NextToken']
unless next_token
next_data = @client.list_inbound_shipments_by_next_token(
next_token
).parse
persist_shipments_then_next(next_data)
end
end
关于ruby-on-rails - 堆栈级别太深,即使递归调用在函数末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40797149/