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



58
59
60
61
62
63
# File 'lib/cont/single_prompt.rb', line 58

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

.prompt0(&block) ⇒ Object



51
52
53
54
55
56
# File 'lib/cont/single_prompt.rb', line 51

def self.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 self.reset(&block)
  prompt0(&block)
end

.run(fiber, *args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cont/single_prompt.rb', line 39

def self.run(fiber, *args)
  status, value = fiber.resume(*args)
  case status
  when :return
    value
  when :capture
    value.call(fiber)
  else
    raise UnexpectedStatusError.new("unexpected status: #{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 self.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