When I have a code that transits through finite states for arbitrary many times as in the following example,
def a
...
case some_condition
when :foo then a
when :bar then b
else c
end
end
def b
...
case some_other_condition
when :baz then a
when :bang then b
else c
end
end
def c
...
case still_another_condition
when :zap then a
when :boom then b
else c
end
end
a
I think that the call stack will grow every time there is a transition to the new state, and that will cause a performance issue. Is there a way to avoid arbitrary growth of the call stack? Is tail recursion optimization related to this?
The first solution that comes into my mind is some kind of routing method:
Then the other methods just return the name of the method to call next:
This will keep the call stack flat. When you want to skip the routing loop, return :end.