Module: SY::SignedMagnitude

Defined in:
lib/sy/signed_magnitude.rb

Overview

Qualities specific to relative magnitudes (mixin).

Instance Method Summary collapse

Instance Method Details

#+(m2) ⇒ Object

Addition.



24
25
26
27
28
29
30
# File 'lib/sy/signed_magnitude.rb', line 24

def + m2
  return magnitude( amount + m2.amount ) if quantity == m2.quantity
  return quantity.absolute.magnitude( amount + m2.amount ) if
    quantity.absolute == m2.quantity
  compat_1, compat_2 = m2.coerce self
  return compat_1 + compat_2
end

#-(m2) ⇒ Object

Subtraction.



34
35
36
37
38
39
40
# File 'lib/sy/signed_magnitude.rb', line 34

def - m2
  return magnitude( amount - m2.amount ) if m2.quantity == quantity.relative
  return quantity.relative.magnitude( amount - m2.amount ) if
    quantity == m2.quantity
  compat_1, compat_2 = m2.coerce self
  return compat_1 - compat_2
end

#initialize(of: nil, amount: nil) ⇒ Object

Relative magnitude constructor takes :quantity (alias :of) argument and :amount argument. Amount is allowed to be negative.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sy/signed_magnitude.rb', line 9

def initialize( of: nil, amount: nil )
  fail ArgumentError, "Quantity (:of) argument missing!" if of.nil?
  @quantity = of
  @amount = case amount
            when Numeric then amount
            when nil then 1
            else
              begin
                amount.( @quantity ).amount
              rescue NameError, NoMethodError; amount end
            end
end