class Comparison::Comparator

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

Attributes

m[R]

rubocop:enable Naming/UncommunicativeMethodParamName

n[R]

rubocop:enable Naming/UncommunicativeMethodParamName

Public Class Methods

new(m, n) click to toggle source

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

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

rubocop:disable Naming/UncommunicativeMethodParamName

# File lib/comparison/comparator.rb, line 19
def initialize(m, n)
  @m = m.to_d
  @n = n.to_d
end

Public Instance Methods

absolute() click to toggle source

Returns the difference between +@m+ and +@n+.

# File lib/comparison/comparator.rb, line 31
def absolute
  @absolute ||= m - n
end
Also aliased as: difference
difference()
Alias for: absolute
percentage()
Alias for: relative
relative() click to toggle source

Returns the percentage difference of +@m+ to +@n+.

# File lib/comparison/comparator.rb, line 39
def relative
  @relative ||= if n.negative?
                  (1 - m / n) * 100
                else
                  (m / n - 1) * 100
                end
end
Also aliased as: percentage