2016-11-25 29 views
2

我下面的答案here的建議,並已證實:tailcall_optimization =>真實:trace_instruction =>假的,但我仍然得到:即使遞歸調用在函數結束時堆棧級別太深?

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中以下和它的作品WTF?:

RubyVM::InstructionSequence.compile_option = { 
    tailcall_optimization: true, 
    trace_instruction: false 
} 
+0

http://www.rubyappcare.com/blog/2016- 09-01-debugging-stack-level-too-deep-error-on-rails希望這將有助於弄清楚錯誤的確切原因。 –

+0

選中此項:http://nithinbekal.com/posts/ruby-tco/。我認爲是'tailcall_optimizer'是什麼導致問題 –

回答

0

你需要有某種形式的遞歸函數調用結束條件。你已經把該條件爲unless next_token.nil?,但你肯定next_token成爲當你試圖終止零,可能是你需要做的僅僅unless 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 
+0

一旦沒有更多的數據要檢索,'NextToken'將會從'prev_data'中消失。我已經在rails控制檯中手動確認了這一點。 – Tony