Please help. I’m totally confused by following behaviour with regard to
continuations (Ruby 1.8.7) My goal was to simply jump between two loops,
which worked fine. But I do not really understand this behaviour:
def loller(id, num, that)
loop do
puts id.to_s + " " + num.to_s
num += 1
sleep(0.5)
# why do i need to update 'that' here, but not 'well' in the
while loop??
# callcc { |here| that.call(here) }, w/o assignment doesn’t work
here
that = callcc { |here| that.call(here) }
# any subsequent calls to 'well' (well.call) should return to
THIS POINT
# with num == 35
# since I never reassign the continuation referenced by ‘well’
end
end
i = 0
well = callcc { |c| loller(1,34,c) }
while true
# why don't I need: well = callcc { |here| well.call(here) }
# callcc { |here| well.call(here) }, w/o assignment works here!!!
callcc { |here| well.call(here) }
puts "back to root " + i.to_s
i += 1
end
This program increases a simple counter in each loop, and prints it.
However, I do not understand why this works, since I am not reassigning
the continuation returned by the loop in function “loller”. The call
made from the while-loop was looking like this to begin with:
well = callcc { |here| well.call(here) }
Which gives the same result, even when i remove the assignment. That
means the ‘well’ variable still references the original continuation
i.e. value of ‘num’ inside loller should always be the same as in
original continuation? Should I remove the analogous assignment from the
loop in “loller” then as expected i never get “back to root” printout,
since then “loller” always returns to the point after the original
assignment, and thst is just before the while loop.
Anybody who understand this, please explain. Big thanks in advance.
/Paul W Florczykowski