Module: SinglePrompt

Included in:
Cont
Defined in:
lib/cont/single_prompt.rb

Defined Under Namespace

Classes: DeadContinuationError, UnexpectedStatusError

Class Method Summary collapse

Class Method Details

.control0(&block) ⇒ Object



55
56
57
58
59
60
# File 'lib/cont/single_prompt.rb', line 55

def control0(&block)
  status, f = Fiber.yield(:capture, block)
  raise UnexpectedStatusError.new("unexpected status: #{status}") \
    unless status == :resume
  f.call()
end

.prompt0(&block) ⇒ Object



48
49
50
51
52
53
# File 'lib/cont/single_prompt.rb', line 48

def prompt0(&block)
  fiber = Fiber.new do
    Fiber.yield(:return, block.call())
  end
  run(fiber)
end

.reset {|block| ... } ⇒ Object

Limit the continuation to the current block.

Yields:

  • (block)

    The block of code to be run

Returns:

  • (Object)

    The result of the block.



16
17
18
# File 'lib/cont/single_prompt.rb', line 16

def reset(&block)
  prompt0(&block)
end

.run(fiber, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/cont/single_prompt.rb', line 37

def run(fiber, *args)
  case fiber.resume(*args)
  in :return, value
    value
  in :capture, value
    value.call(fiber)
  else
    raise UnexpectedStatusError.new("unexpected status")
  end
end

.shift {|block| ... } ⇒ Object

Capture the current continuation.

Yields:

  • (block)

    The block of code to be run

Returns:

  • (Object)

    The result of the block.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cont/single_prompt.rb', line 24

def shift(&block)
  control0 do |fiber|
    prompt0 do
      block.call lambda { |value|
        prompt0 do
          raise DeadContinuationError.new unless fiber.alive?
          run(fiber, :resume, lambda { value })
        end
      }
    end
  end
end