Module: Scalar::SupportClasses::Measurement

Includes:
Comparable
Included in:
Length, Weight
Defined in:
lib/scalar/support_classes/measurement.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scalarObject Also known as: abs

Returns the value of attribute scalar.



70
71
72
# File 'lib/scalar/support_classes/measurement.rb', line 70

def scalar
  @scalar
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
# File 'lib/scalar/support_classes/measurement.rb', line 28

def self.included(base)
  base.extend ClassMethods
  # .new is not a necessary public method
  base.private_class_method :new
end

Instance Method Details

#+(other) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/scalar/support_classes/measurement.rb', line 52

def +(other)
  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) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/scalar/support_classes/measurement.rb', line 44

def -(other)
  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) ⇒ Object



34
35
36
37
# File 'lib/scalar/support_classes/measurement.rb', line 34

def <=>(other)
  return nil unless other.is_a?(self.class)
  scalar <=> other.send(unit).scalar
end

#initialize(scalar, unit) ⇒ Object



60
61
62
63
# File 'lib/scalar/support_classes/measurement.rb', line 60

def initialize(scalar, unit)
  self.scalar = scalar.to_r
  self.unit = unit
end

#inspectObject



65
66
67
68
# File 'lib/scalar/support_classes/measurement.rb', line 65

def inspect
  hex_id = (object_id << 1).to_s(16).rjust(14, '0')
  "#<#{self.class.name}:0x#{hex_id}  #{scalar.to_f} #{unit}>"
end

#succObject

this is part of making Weight ranges work.



40
41
42
# File 'lib/scalar/support_classes/measurement.rb', line 40

def succ
  self.class.send(unit, scalar + 1)
end