class MonotonicTickCount

Constants

VERSION

Attributes

tick_count_f[R]

the tick count in seconds as floating point at nanosecond granularity

Public Class Methods

new(other = nil, tick_count_f: nil) click to toggle source

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
# 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
now() click to toggle source
# File lib/monotonic_tick_count.rb, line 59
def now
  new(tick_count_f: Process.clock_gettime(Process::CLOCK_MONOTONIC))
end
timer() { |start_tick| ... } click to toggle source

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

# File lib/monotonic_tick_count.rb, line 64
def timer
  start_tick = self.now
  result = yield(start_tick)
  [result, self.now - start_tick]
end

Public Instance Methods

+(other) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# 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
hash() click to toggle source
# File lib/monotonic_tick_count.rb, line 52
def hash
  @tick_count_f.hash
end
inspect() click to toggle source
# File lib/monotonic_tick_count.rb, line 26
def inspect
  "monotonic tick count #{@tick_count_f}"
end