module Scalar::SupportClasses::Measurement

Attributes

abs[R]
scalar[RW]
to_r[R]
unit[RW]

Public Class Methods

included(base) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 30
def self.included(base)
  base.extend ClassMethods
  # .new is not a necessary public method
  base.private_class_method :new
end
new(scalar, unit) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 80
def initialize(scalar, unit)
  self.scalar = scalar.to_r
  self.unit = unit
end

Public Instance Methods

*(other) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 64
def *(other)
  if !other.is_a?(Numeric)
    raise TypeError, "#{other.class} can't be coerced into #{scalar.class}"
  end

  self.class.send(unit, scalar * other)
end
+(other) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 55
def +(other)
  other = self.class.send(unit, 0) if other == 0
  if !other.is_a?(self.class)
    raise TypeError, "#{other.class} can't be coerced into #{self.class}"
  end

  self.class.send(unit, scalar + other.send(unit).scalar)
end
-(other) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 46
def -(other)
  other = self.class.send(unit, 0) if other == 0
  if !other.is_a?(self.class)
    raise TypeError, "#{other.class} can't be coerced into #{self.class}"
  end

  self.class.send(unit, scalar - other.send(unit).scalar)
end
/(other) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 72
def /(other)
  if !other.is_a?(Numeric)
    raise TypeError, "#{other.class} can't be coerced into #{scalar.class}"
  end

  self.class.send(unit, scalar / other)
end
<=>(other) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 36
def <=>(other)
  return nil unless other.is_a?(self.class)
  scalar <=> other.send(unit).scalar
end
inspect() click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 85
def inspect
  hex_id = (object_id << 1).to_s(16).rjust(14, '0')
  "#<#{self.class.name}:0x#{hex_id}  #{scalar.to_f} #{unit}>"
end
succ() click to toggle source

this is part of making Weight ranges work.

# File lib/scalar/support_classes/measurement.rb, line 42
def succ
  self.class.send(unit, scalar + 1)
end

Private Instance Methods

convert(from:, to:) click to toggle source
# File lib/scalar/support_classes/measurement.rb, line 100
def convert(from:, to:)
  converted_scalar = self.class::CONVERSIONS.call(from: from, to: to) * scalar
  self.class.send(to, converted_scalar)
end