Class: Ruff::Standard::Defer::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/ruff/standard/defer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstance

makes a new instance.



11
12
13
# File 'lib/ruff/standard/defer.rb', line 11

def initialize
  @eff = Ruff.instance
end

Instance Attribute Details

#effObject (readonly)

You can reimplement the handler using this effect instance.



52
53
54
# File 'lib/ruff/standard/defer.rb', line 52

def eff
  @eff
end

Instance Method Details

#register(&prc) ⇒ ()

is a smart method to invoke the effect operation.

Parameters:

  • prc (Proc<(), ()>)

    is deferred, or "registerred", and called on the end of computation.

Returns:

  • (())


19
20
21
# File 'lib/ruff/standard/defer.rb', line 19

def register(&prc)
  @eff.perform prc
end

#with(&th) ⇒ A!{e}

is a handler to interpret the effect invocation as registering a procedure. Registerred procedures are run on the end of computation, by value handler.

Parameters:

  • th (Proc<(), A>!{Defer.eff, e})

    is a thunk returning A with the possibility to invoke effects, including Defer.eff .

Returns:

  • (A!{e})

    returns A , without modification by value handler. But it still has the possibility to invoke effects(e).



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruff/standard/defer.rb', line 32

def with(&th)
  # This is a stack to store deferred procedures.
  procs = []

  # The handler *closes* `procs` variable so it should be created every time.
  Ruff.handler
      .on(@eff) do |k, prc|
    procs << prc
    k[]
  end
      .to do |v|
    # Like Go's defer functions, it crashes the thunk by reversed order.
    procs.reverse_each(&:[])

    v
  end
      .run(&th)
end