From: Benjamin Thomas Date: 2009-03-12T05:45:42+09:00 Subject: Re: return doesn't always exit my method Christopher Dicely wrote: > Your intuition on where the problem is is correct. Note that when > using recursion, each call to the function is a separate "layer", and > calling return only jumps you out of the current layer. Thanks very much Christopher! I wasn't aware of this at all. I did run the code changes you provided and it helps! I think I grasp the general concept but some aspects remain a mistery. Mainly I do not understand what the code is doing when it reverts to a previous "layer": If the hash is new and fairly empty, the layers pop off in cascading style. No other bit of code seems to get executed. this is the output I get when I type 'exit' ##########console output############# creation! iteration 0 1*8 = exit will exit now returning to rand_multi(12) returning to rand_multi(11) returning to rand_multi(10) returning to rand_multi(9) returning to rand_multi(8) returning to rand_multi(7) returning to rand_multi(6) returning to rand_multi(5) returning to rand_multi(4) returning to rand_multi(3) returning to rand_multi(2) returning to rand_multi(1) returning to rand_multi(0) Bad answers: 7*6 => -1 ###################################### But if the hash gets populated, the first part of the function gets executed from time to time and 'return' "sticks" to some layers. This is the output I get then: ###############console output################### 6*1 = exit will exit now returning to rand_multi(30) returning to rand_multi(29) iteration 2 6*7 = exit will exit now returning to rand_multi(28) iteration 2 6*7 = exit will exit now returning to rand_multi(27) iteration 2 1*8 = exit will exit now returning to rand_multi(26) returning to rand_multi(25) returning to rand_multi(24) returning to rand_multi(23) iteration 2 1*3 = ################################################# I would guess return brings me back to the exact same spot the recursive call got called. Is that correct? And in that case, wouldn't that mean that my function logic is fundamentally flawed because unpredictable? Thanks very much Matt for your suggestion. Unfortunatly I cannot apply it here because I've barely touched procs and only have a vague idea of what throw and catch do. But it's definitly material for me to study so thanks! Thank you also Udayanga. On first analysis, it's true that my "termination logic" was to input 'exit' at the prompt but I have also an infinite loop going on when I have answered all possible operations correctly with a score of 2 (the badly named variable 'iteration'). -- Posted via http://www.ruby-forum.com/.