From: Pit Capitain Date: 2001-08-22T21:06:30+09:00 Subject: [ruby-talk:20109] Re: breaking out of nested loops Hi Brian, does your continuation example really work for you? On Win NT Ruby 1.6.4 I get match: 2 match: 3 nomatch x.rb:28: undefined method `call' for nil(NameError) from x.rb:28 from x.rb:0 The code (with line numbers) is 23 val, restart = find_match_3(0..3, 2..5) 24 puts val 25 # => match: 2 26 27 # Hmm, don't like that match. 28 val, restart = restart.call 29 puts val 30 # => match: 3 31 32 # Don't like that one either. 33 val, restart = restart.call 34 puts val 35 # => nomatch In line 28, when the resume_continuation is called for the first time, it jumps back into the call to method find_match_3 (which is in line 23). After returning from the method call, the code continues in line 24 again, which after another iteration leads to the error shown above. Just curious... BTW, you could write method return_resumably as: def return_resumably(primary_retval, return_continuation) callcc do | resume_continuation | return_continuation.call(primary_retval, resume_continuation) end end ...but this doesn't correct the error. I've used continuations successfully before more or less like you did. I'll try to find the differences between your code and mine later. Pit On 22 Aug 2001, at 2:44, Brian Marick wrote: > (...) > > Here's the code: > > def find_match_3(first_range, next_range) > callcc do | out | > for i in first_range > for j in next_range > if i == j > return_resumably("match: #{i}", out) # CHANGE > end > end > end > "nomatch" > end > end > > # Just like the process switch code in the V6 Unix kernel! > def return_resumably(primary_retval, return_continuation) > returning_now = true > resume_continuation = nil > callcc { | resume_continuation |; } > if returning_now > returning_now = false > return_continuation.call(primary_retval, resume_continuation) > end > end > > I wonder if there's a cleaner way to do this, but I must do some > paying work now. >