Programmierung in Ruby

Der Leitfaden der Pragmatischen Programmierer

class Continuation
Parent: Object
Version: 1.6

Index:

call


Continuation-Objekte werden von Kernel#callcc generiert. Sie beinhalten eine return-Addresse sowie einen Ausführungs-Kontext, der eine nicht-lokale Rückkehr an das Ende des callcc-Blocks von jeder Programmstelle aus ermöglicht. Continuations sind etwa analog zu einer strukturierten Version von C's setjmp/longjmp (obwohl sie mehr Zustandsinformationen enthalten, so dass sie sehr ähnlich zu Threads sind).

Zum Beispiel:

arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
callcc{|$cc|}
puts(message = arr.shift)
$cc.call unless message =~ /Max/
ergibt:
Freddie
Herbie
Ron
Max

Dieses (etwas "contreived") Beispiel erlaubt es der inneren Schleife, die Ausführung früher zu beenden.

callcc {|cont|
  for i in 0..4
    print "\n#{i}: "
    for j in i*5...(i+1)*5
      cont.call() if j == 17
      printf "%3d", j
    end
  end
}
print "\n"
produces:

0:   0  1  2  3  4 1:   5  6  7  8  9 2:  10 11 12 13 14 3:  15 16

instance methods
call cont.call( [args]* )

Diese Methode ruft das Continuation-Objekt auf. Das Programm nimmt die Ausführung am Ende des callcc-Blocks wieder auf. Wenn keine Argumente übergeben werden, gibt das ursprüngliche callccnil zurück. Wenn ein Argument angegeben ist, gibt callcc dieses zurück. In anderen Fällen wird ein Array, das args enthält, zurück gegeben.

callcc {|cont|  cont.call } » nil
callcc {|cont|  cont.call 1 } » 1
callcc {|cont|  cont.call 1, 2, 3 } » [1, 2, 3]


Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Übersetzung: Carsten Schinzer
Für das englische Original:
© 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. That reference is available for download.
Diese Lizenz sowie das Original vom Herbst 2001 bilden die Grundlage der Übersetzung
Es wird darauf hingewiesen, dass sich die Lizenz des englischen Originals inzwischen geändert hat.
Für die deutsche Übersetzung:
© 2002 Jürgen Katins
Der Copyright-Eigner stellt folgende Lizenzen zur Verfügung:
Nicht-freie Lizenz:
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder. Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.
Freie Lizenz:
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".