Class: Comparison::Comparator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/comparison/comparator.rb

Overview

The Comparator object compares two numbers to each other and exposes the raw and percentage differences.

Direct Known Subclasses

Presenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, n) ⇒ Comparator

Instantiates a new Comparator to compare two numbers, m and n.

Both numbers will be converted to instances of ‘BigDecimal`.

rubocop:disable Naming/UncommunicativeMethodParamName



19
20
21
22
# File 'lib/comparison/comparator.rb', line 19

def initialize(m, n)
  @m = m.to_d
  @n = n.to_d
end

Instance Attribute Details

#mObject (readonly)

rubocop:enable Naming/UncommunicativeMethodParamName



25
26
27
# File 'lib/comparison/comparator.rb', line 25

def m
  @m
end

#nObject (readonly)

rubocop:enable Naming/UncommunicativeMethodParamName



25
26
27
# File 'lib/comparison/comparator.rb', line 25

def n
  @n
end

Instance Method Details

#absoluteObject Also known as: difference

Returns the difference between @m and @n.



31
32
33
# File 'lib/comparison/comparator.rb', line 31

def absolute
  @absolute ||= m - n
end

#relativeObject Also known as: percentage

Returns the percentage difference of @m to @n.



39
40
41
42
43
44
45
# File 'lib/comparison/comparator.rb', line 39

def relative
  @relative ||= if n.negative?
                  (1 - m / n) * 100
                else
                  (m / n - 1) * 100
                end
end