continuation

Discussion in 'Ruby' started by Ross Bamford, Feb 5, 2006.

  1. Ross Bamford

    Ross Bamford Guest

    On Sun, 05 Feb 2006 02:49:58 -0000, Schüle Daniel
    <-karlsruhe.de> wrote:

    > 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'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.

    --
    Ross Bamford -
    Ross Bamford, Feb 5, 2006
    #1
    1. Advertising

  2. Ross Bamford

    angus Guest

    [Sch=FCle Daniel <-karlsruhe.de>, 2006-02-05 01.58 CET]
    > Hello,
    >=20
    > irb(main):005:0* cnt=3D0
    > =3D> 0
    > irb(main):006:0> cc=3Dnil
    > =3D> nil
    > irb(main):007:0> callcc {|cc|}
    > =3D> nil
    > irb(main):008:0> cnt+=3D1
    > =3D> 1
    > irb(main):009:0> cc.call if cnt < 10
    > =3D> nil
    > irb(main):010:0> cnt
    > =3D> 1
    > irb(main):011:0>
    > irb(main):012:0* RUBY_VERSION
    > =3D> "1.8.4"
    > irb(main):013:0>
    >=20
    > this example is from
    > http://www.rubygarden.org/ruby?Continuations
    > 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.

    --=20
    angus, Feb 5, 2006
    #2
    1. Advertising

  3. 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
    Schüle Daniel, Feb 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    2
    Views:
    1,912
    Mario Vargas
    Jul 17, 2003
  2. DC

    Continuation question....

    DC, May 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    446
  3. aarthy kumar
    Replies:
    3
    Views:
    379
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Oct 12, 2005
  4. TheDustbustr

    stackless python: continuation module?

    TheDustbustr, Jul 25, 2003, in forum: Python
    Replies:
    2
    Views:
    452
    Christian Tismer
    Aug 6, 2003
  5. Sandra-24
    Replies:
    5
    Views:
    396
    Leif K-Brooks
    Apr 9, 2006
Loading...

Share This Page