still fairly new to ruby, and wrote this very simple recursive function.
def test(input)
if input != 0
test(input-1)
end
if input == 0
return true
end
end
puts test(5)
from my Java knowledge I know this should return true but it doesn’t. It seems like the return statement doesn’t actually break out of the method. how do I fix this? thanks
If you watch carefully you will see that the method does in fact return, but it only unwinds the stack one level and continues executing code in the caller.
The problem is that you forgot a return:
With this fix, the result is as expected:
See it working online: ideone