Class: Comparison::Comparator
- Inherits:
-
Object
- Object
- Comparison::Comparator
- 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
Instance Attribute Summary collapse
-
#m ⇒ Object
readonly
rubocop:enable Naming/UncommunicativeMethodParamName.
-
#n ⇒ Object
readonly
rubocop:enable Naming/UncommunicativeMethodParamName.
Instance Method Summary collapse
-
#absolute ⇒ Object
(also: #difference)
Returns the difference between @m and @n.
-
#initialize(m, n) ⇒ Comparator
constructor
Instantiates a new Comparator to compare two numbers,
mandn. -
#relative ⇒ Object
(also: #percentage)
Returns the percentage difference of @m to @n.
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
#m ⇒ Object (readonly)
rubocop:enable Naming/UncommunicativeMethodParamName
25 26 27 |
# File 'lib/comparison/comparator.rb', line 25 def m @m end |
#n ⇒ Object (readonly)
rubocop:enable Naming/UncommunicativeMethodParamName
25 26 27 |
# File 'lib/comparison/comparator.rb', line 25 def n @n end |
Instance Method Details
#absolute ⇒ Object 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 |
#relative ⇒ Object 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 |