Class: MonotonicTickCount

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/monotonic_tick_count.rb,
lib/monotonic_tick_count/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(other = nil, tick_count_f: nil) ⇒ MonotonicTickCount

initialize from one of:

- another object of this type OR
- an equivalent object that responds to tick_count_f OR
- an explicit keyword value of tick_count_f: which is a floating point count of seconds with fractional second at nanosecond granularity


17
18
19
20
21
22
23
24
# File 'lib/monotonic_tick_count.rb', line 17

def initialize(other = nil, tick_count_f: nil)
  @tick_count_f = if other
                    other.respond_to?(:tick_count_f) or raise ArgumentError, "Must initialize from #{self.class} or equivalent"
                    other.tick_count_f
                  else
                    tick_count_f or raise ArgumentError, "Must provide either other or tick_count_f:"
                  end
end

Instance Attribute Details

#tick_count_fObject (readonly)

the tick count in seconds as floating point at nanosecond granularity



11
12
13
# File 'lib/monotonic_tick_count.rb', line 11

def tick_count_f
  @tick_count_f
end

Class Method Details

.nowObject



59
60
61
# File 'lib/monotonic_tick_count.rb', line 59

def now
  new(tick_count_f: Process.clock_gettime(Process::CLOCK_MONOTONIC))
end

.timerObject

yields to the caller and returns a pair: [result from yield, float time in seconds of block run]



64
65
66
67
68
# File 'lib/monotonic_tick_count.rb', line 64

def timer
  start_tick = self.now
  result = yield(start_tick)
  [result, self.now - start_tick]
end

Instance Method Details

#+(other) ⇒ Object



42
43
44
45
# File 'lib/monotonic_tick_count.rb', line 42

def +(other)
  other.respond_to?(:to_f) or raise ArgumentError, "Other operand must respond to to_f"
  self.class.new(tick_count_f: @tick_count_f + other.to_f)
end

#-(other) ⇒ Object

When the RHS is a convertible to float, returns an offset to the current tick count When the RHS is a MonotonicTickCount, returns the difference in float seconds



32
33
34
35
36
37
38
39
40
# File 'lib/monotonic_tick_count.rb', line 32

def -(other)
  if other.respond_to?(:tick_count_f)
    @tick_count_f - other.tick_count_f
  elsif other.respond_to?(:to_f)
    self + -other
  else
    raise ArgumentError, "Other operand must be another #{self.class} or respond to to_f"
  end
end

#<=>(other) ⇒ Object



47
48
49
50
# File 'lib/monotonic_tick_count.rb', line 47

def <=>(other)
  other.respond_to?(:tick_count_f) or raise ArgumentError, "Other operand must be a #{self.class} or equivalent"
  @tick_count_f <=> other.tick_count_f
end

#hashObject



52
53
54
# File 'lib/monotonic_tick_count.rb', line 52

def hash
  @tick_count_f.hash
end

#inspectObject



26
27
28
# File 'lib/monotonic_tick_count.rb', line 26

def inspect
  "monotonic tick count #{@tick_count_f}"
end