I’ve start getting acquainted with Ruby and met a function never seen before — callcc.
I’ve understood some general idea of what’s it for but when i had tried to write an example and i’ve got unexpected result.
require 'continuation'
def callcc_func
i = 0
while true
c = nil
callcc {|x| c = x}
i += 1
puts i
return c if (i % 3) == 0
end
end
c = callcc_func()
puts
callcc_func.call
The result is an endless loop. Why?
I’ve expected it to be:
# for `c = callcc_func()` line
1
2
3
# callcc_func.call
4
5
6
#end here because of `return c if (i % 3) == 0`
P.S.
Sorry for my English and thanks.
call-with-current-continuation(generally abbreviated ascall/ccoriginated in Scheme, so if you want to know more about it, Scheme docs are a good starting point:As for your Ruby problem: look at this blog post title Continuations and ruby, it implements something very similar to what you are trying to do. You’ll find an explanation there: