In Files
- cont.c
Files
- grammar.en.rdoc
- test.ja.rdoc
- contributing.rdoc
- contributors.rdoc
- dtrace_probes.rdoc
- extension.ja.rdoc
- extension.rdoc
- globals.rdoc
- keywords.rdoc
- maintainers.rdoc
- marshal.rdoc
- regexp.rdoc
- security.rdoc
- standard_library.rdoc
- syntax.rdoc
- assignment.rdoc
- calling_methods.rdoc
- control_expressions.rdoc
- exceptions.rdoc
- literals.rdoc
- methods.rdoc
- miscellaneous.rdoc
- modules_and_classes.rdoc
- precedence.rdoc
- refinements.rdoc
- README.ja.rdoc
- README.rdoc
Class/Module Index
- ArgumentError
- Array
- BasicObject
- Bignum
- Binding
- Class
- ClosedQueueError
- Comparable
- Complex
- Complex::compatible
- ConditionVariable
- Continuation
- Data
- Dir
- ENV
- EOFError
- Encoding
- Encoding::CompatibilityError
- Encoding::Converter
- Encoding::ConverterNotFoundError
- Encoding::InvalidByteSequenceError
- Encoding::UndefinedConversionError
- EncodingError
- Enumerable
- Enumerator
- Enumerator::Generator
- Enumerator::Lazy
- Enumerator::Yielder
- Errno
- Exception
- FalseClass
- Fiber
- FiberError
- File
- File::Constants
- File::Stat
- FileTest
- Fixnum
- Float
- FloatDomainError
- GC
- GC::Profiler
- Hash
- IO
- IO::EAGAINWaitReadable
- IO::EAGAINWaitWritable
- IO::EINPROGRESSWaitReadable
- IO::EINPROGRESSWaitWritable
- IO::EWOULDBLOCKWaitReadable
- IO::EWOULDBLOCKWaitWritable
- IO::WaitReadable
- IO::WaitWritable
- IOError
- IndexError
- Integer
- Interrupt
- Kernel
- KeyError
- LoadError
- LocalJumpError
- Marshal
- MatchData
- Math
- Math::DomainError
- Method
- Module
- NameError
- NilClass
- NoMemoryError
- NoMethodError
- NotImplementedError
- Numeric
- Object
- ObjectSpace
- ObjectSpace::WeakMap
- Proc
- Process
- Process::GID
- Process::Status
- Process::Sys
- Process::UID
- Process::Waiter
- Queue
- Random
- Random::Formatter
- Range
- RangeError
- Rational
- Rational::compatible
- Regexp
- RegexpError
- RubyVM
- RubyVM::Env
- RubyVM::InstructionSequence
- RuntimeError
- ScriptError
- SecurityError
- Signal
- SignalException
- SizedQueue
- StandardError
- StopIteration
- String
- Struct
- Symbol
- SyntaxError
- SystemCallError
- SystemExit
- SystemStackError
- Thread
- Thread::Backtrace::Location
- Thread::Mutex
- ThreadError
- ThreadGroup
- Time
- TracePoint
- TrueClass
- TypeError
- UnboundMethod
- UncaughtThrowError
- ZeroDivisionError
- fatal
- unknown
Continuation
Continuation objects are generated by Kernel#callcc, after having
+require+d continuation. They hold a return address and execution
context, allowing a nonlocal return to the end of the callcc
block from anywhere within a program. Continuations are somewhat analogous
to a structured version of C's setjmp/longjmp
(although they
contain more state, so you might consider them closer to threads).
For instance:
require "continuation" arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] callcc{|cc| $cc = cc} puts(message = arr.shift) $cc.call unless message =~ /Max/
produces:
Freddie Herbie Ron Max
Also you can call callcc in other methods:
require "continuation" def g arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] cc = callcc { |cc| cc } puts arr.shift return cc, arr.size end def f c, size = g c.call(c) if size > 1 end f
This (somewhat contrived) example allows the inner loop to abandon processing early:
require "continuation" 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 } puts
produces:
0: 0 1 2 3 4 1: 5 6 7 8 9 2: 10 11 12 13 14 3: 15 16
Public Instance Methods
Invokes the continuation. The program continues from the end of the
callcc
block. If no arguments are given, the original
callcc
returns nil
. If one argument is given,
callcc
returns it. Otherwise, an array containing
args is returned.
callcc {|cont| cont.call } #=> nil callcc {|cont| cont.call 1 } #=> 1 callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
static VALUE rb_cont_call(int argc, VALUE *argv, VALUE contval) { rb_context_t *cont; rb_thread_t *th = GET_THREAD(); GetContPtr(contval, cont); if (cont->saved_thread.self != th->self) { rb_raise(rb_eRuntimeError, "continuation called across threads"); } if (cont->saved_thread.protect_tag != th->protect_tag) { rb_raise(rb_eRuntimeError, "continuation called across stack rewinding barrier"); } if (cont->saved_thread.fiber) { if (th->fiber != cont->saved_thread.fiber) { rb_raise(rb_eRuntimeError, "continuation called across fiber"); } } rollback_ensure_stack(contval, th->ensure_list, cont->ensure_array); cont->argc = argc; cont->value = make_passing_arg(argc, argv); /* restore `tracing' context. see [Feature #4347] */ th->trace_arg = cont->saved_thread.trace_arg; cont_restore_0(cont, &contval); return Qnil; /* unreachable */ }
Invokes the continuation. The program continues from the end of the
callcc
block. If no arguments are given, the original
callcc
returns nil
. If one argument is given,
callcc
returns it. Otherwise, an array containing
args is returned.
callcc {|cont| cont.call } #=> nil callcc {|cont| cont.call 1 } #=> 1 callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
static VALUE rb_cont_call(int argc, VALUE *argv, VALUE contval) { rb_context_t *cont; rb_thread_t *th = GET_THREAD(); GetContPtr(contval, cont); if (cont->saved_thread.self != th->self) { rb_raise(rb_eRuntimeError, "continuation called across threads"); } if (cont->saved_thread.protect_tag != th->protect_tag) { rb_raise(rb_eRuntimeError, "continuation called across stack rewinding barrier"); } if (cont->saved_thread.fiber) { if (th->fiber != cont->saved_thread.fiber) { rb_raise(rb_eRuntimeError, "continuation called across fiber"); } } rollback_ensure_stack(contval, th->ensure_list, cont->ensure_array); cont->argc = argc; cont->value = make_passing_arg(argc, argv); /* restore `tracing' context. see [Feature #4347] */ th->trace_arg = cont->saved_thread.trace_arg; cont_restore_0(cont, &contval); return Qnil; /* unreachable */ }