How to increase stack size for a ruby app. Recursive app getting: Stack level too deep (SystemStackError)

前端 未结 7 1831
感情败类
感情败类 2020-11-30 02:53

Posting a stack overflow question on stackoverflow.com, how amusing :-)

I\'m running some recursive Ruby code and I get the: \"Stack level too deep (SystemStac

7条回答
  •  遥遥无期
    2020-11-30 03:49

    If you are sure that you do not have an Infinite recursion situation then your algorythm is pobably not suited for Ruby to execute it in a recirsive manner. Converting an algorythm from recursion to different kind of stack is pretty easy and I suggest you try that. Here is how you can do it.

    def recursive(params)
      if some_conditions(params)
         recursive(update_params(params))
      end
    end
    
    recursive(starting_params)
    

    will transform into

    stack = [starting_params]
    while !stack.empty?
      current_params = stack.delete_at(0)
      if some_conditions(current_params)
        stack << update_params(current_params)
      end
    end
    

提交回复
热议问题