Class: Ruff::Standard::MeasureTime::Instance

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstance

makes a new instance.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruff/standard/measure_time.rb', line 22

def initialize
  @eff = Ruff.instance
  @handler = Ruff.handler
  @handler.on(@eff) do |k, label|
    t1 = Time.now
    result = k[]
    t2 = Time.now
    result + [{ label: label, time: t2 - t1 }]
  end
  @handler.to { |x| [x] }
end

Instance Attribute Details

#effObject (readonly)

You can reimplement the handler using this effect instance.



56
57
58
# File 'lib/ruff/standard/measure_time.rb', line 56

def eff
  @eff
end

Instance Method Details

#measure(label) ⇒ ()

is a smart method to invoke the effect operation.

Parameters:

  • label (string)

    is the label of the measurement.

Returns:

  • (())


38
39
40
# File 'lib/ruff/standard/measure_time.rb', line 38

def measure(label)
  @eff.perform(label)
end

#with(&th) ⇒ [A, ...{ label: string, time: float }]!{e}

is a handler to interpret the effect invocation as measuring computation time.

Parameters:

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

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

Returns:

  • ([A, ...{ label: string, time: float }]!{e})

    returns list. the first is the result A, and the rest is the measurement results. It still has the possibility to invoke effects(e).



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

def with(&th)
  @handler.run(&th)
end