Class: Ruff::Effect

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

Overview

This class provides an effect instance.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEffect<Arg, Ret>

instaciates an effect setting id.

Examples:

Log = Effect.new #==> it _might_ be Effect<string, nil>


15
16
17
# File 'lib/ruff/effect.rb', line 15

def initialize
  @id = Class.new.new
end

Instance Attribute Details

#idObject (readonly)

Each instance must be unique so they have unique id with an annonymous class instance.

The class instance may have subtyping relation.

See Also:



9
10
11
# File 'lib/ruff/effect.rb', line 9

def id
  @id
end

Class Method Details

.<<(parent) ⇒ Effect<Arg, Ret>

instanciates an effect, which has an relation self <: parent from the subtyping of id object.

Examples:

Exception = Ruff::Effect.new
RuntimeException = Ruff::Effect << Exception

Ruff::Handler.new
  .on(Exception){
    puts "catch"
  }
  .run {
    RuntimeException.perform
  }
# ==> prints "catch"

Parameters:

Returns:

  • (Effect<Arg, Ret>)

    with an relation it <: parent



37
38
39
40
41
42
# File 'lib/ruff/effect.rb', line 37

def self.<<(parent)
  inst = new
  parent_id = parent.instance_variable_get('@id')
  inst.instance_variable_set('@id', (Class.new parent_id.class).new)
  inst
end

Instance Method Details

#perform(*a) ⇒ Ret

sends an effect ID and its arguments to a nearmost handler.

Examples:

Log.perform "hello"

Parameters:

  • a (Arg)

    of the object Effect<Arg, Ret>

Returns:

  • (Ret)

    of the object Effect<Arg, Ret>



51
52
53
# File 'lib/ruff/effect.rb', line 51

def perform(*a)
  Fiber.yield Ruff::Throws::Eff.new(@id, a)
end