Continuation


#1

Hello,

irb(main):005:0* cnt=0
=> 0
irb(main):006:0> cc=nil
=> nil
irb(main):007:0> callcc {|cc|}
=> nil
irb(main):008:0> cnt+=1
=> 1
irb(main):009:0> cc.call if cnt < 10
=> nil
irb(main):010:0> cnt
=> 1
irb(main):011:0>
irb(main):012:0* RUBY_VERSION
=> “1.8.4”
irb(main):013:0>

this example is from
http://www.rubygarden.org/ruby?Continuations
and cnt should become 10

I also tried some other examples from the net
and it worked only in this case

irb(main):014:0* callcc do |cc|
irb(main):015:1* for i in 0…10
irb(main):016:2> print "\n#{i}: "
irb(main):017:2> for j in 0…10
irb(main):018:3> cc.call if j == 5 and i == 5
irb(main):019:3> printf “%3i”, j
irb(main):020:3> end
irb(main):021:2> end
irb(main):022:1> end

0: 0 1 2 3 4 5 6 7 8 9 10
1: 0 1 2 3 4 5 6 7 8 9 10
2: 0 1 2 3 4 5 6 7 8 9 10
3: 0 1 2 3 4 5 6 7 8 9 10
4: 0 1 2 3 4 5 6 7 8 9 10
5: 0 1 2 3 4=> nil
irb(main):023:0>

Maybe someone can help

Regards, Daniel


#2

On Sun, 05 Feb 2006 02:49:58 -0000, Schüle Daniel
removed_email_address@domain.invalid wrote:

irb(main):010:0> cnt
=> 1
irb(main):011:0>
irb(main):012:0* RUBY_VERSION
=> “1.8.4”
irb(main):013:0>

this example is from
http://www.rubygarden.org/ruby?Continuations
and cnt should become 10

I’ve found that certain things like continuations don’t work well under
IRB. I think it’s because of the way it’s workspaces work.

That code works fine as a script.


#3

[Schüle Daniel removed_email_address@domain.invalid, 2006-02-05 01.58 CET]

irb(main):009:0> cc.call if cnt < 10
and cnt should become 10
The problem is IRB. It is a ruby program whose main loop reads your
input,
evaluates it, and shows you the result, so when you call the
continuation,
it only returns to the point where it shows you the result of your
input;
it doesn’t evaluate the statementes you entered later.

Try the example outside IRB.