I have a ruby service that keeps running forever and I wonder whats the cost of recursivity. When I ctrl-c the service after some time, I get the following error printed:
^Cff.rb:169:in `sleep': Interrupt
from ff.rb:169:in `fetch'
from ff.rb:170:in `fetch'
from ff.rb:187:in `fetch'
from ff.rb:180:in `fetch'
from ff.rb:170:in `fetch'
开发者_如何学C from ff.rb:187:in `fetch'
from ff.rb:177:in `fetch'
from ff.rb:170:in `fetch'
.... and continue for each recursive call
This makes me wonder if this has a memory cost or if it eventually will fail? Is it bad to use recursive in Ruby like this? Would another solution be better? Ty.
AFIAK, Ruby never turns tail call recursion into loops. If you keep calling a function recursively, eventually you'll run out of memory.
精彩评论