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
-
#other ⇒ Object
readonly
Returns the value of attribute other.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #absolute ⇒ Object
-
#change ⇒ Object
Returns the relative change of @value from @other.
-
#difference ⇒ Object
Returns the difference between @value and @other.
-
#initialize(value, other) ⇒ Comparator
constructor
Instantiates a new Comparator to compare two numbers,
valueandother. - #m ⇒ Object
- #n ⇒ Object
-
#percentage ⇒ Object
Returns the relative change of @value from @other as a percentage.
- #relative ⇒ Object
Constructor Details
#initialize(value, other) ⇒ Comparator
Instantiates a new Comparator to compare two numbers, value and other.
Both numbers will be converted to instances of ‘BigDecimal`.
19 20 21 22 |
# File 'lib/comparison/comparator.rb', line 19 def initialize(value, other) @value = value.to_d @other = other.to_d end |
Instance Attribute Details
#other ⇒ Object (readonly)
Returns the value of attribute other.
13 14 15 |
# File 'lib/comparison/comparator.rb', line 13 def other @other end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
13 14 15 |
# File 'lib/comparison/comparator.rb', line 13 def value @value end |
Instance Method Details
#absolute ⇒ Object
33 34 35 36 |
# File 'lib/comparison/comparator.rb', line 33 def absolute Kernel.warn "DEPRECATION WARNING: use #difference instead of #absolute (called from #{caller(1..1).first})" difference end |
#change ⇒ Object
Returns the relative change of @value from @other.
40 41 42 |
# File 'lib/comparison/comparator.rb', line 40 def change difference / other.abs end |
#difference ⇒ Object
Returns the difference between @value and @other.
29 30 31 |
# File 'lib/comparison/comparator.rb', line 29 def difference value - other end |
#m ⇒ Object
55 56 57 58 |
# File 'lib/comparison/comparator.rb', line 55 def m Kernel.warn "DEPRECATION WARNING: use #value instead of #m (called from #{caller(1..1).first})" value end |
#n ⇒ Object
60 61 62 63 |
# File 'lib/comparison/comparator.rb', line 60 def n Kernel.warn "DEPRECATION WARNING: use #other instead of #n (called from #{caller(1..1).first})" other end |
#percentage ⇒ Object
Returns the relative change of @value from @other as a percentage.
46 47 48 |
# File 'lib/comparison/comparator.rb', line 46 def percentage change * 100 end |
#relative ⇒ Object
50 51 52 53 |
# File 'lib/comparison/comparator.rb', line 50 def relative Kernel.warn "DEPRECATION WARNING: use #percentage instead of #relative (called from #{caller(1..1).first})" change end |