Module: Amount::Comparison
Overview
Comparison, equality, hashing, and sign predicates for ‘Amount`.
Pulls in ‘Comparable` itself so consumers only need `include Comparison` to get `<`, `<=`, `>`, `>=`, `between?`, `clamp`, and `Enumerable#min`/ `#max` alongside the explicit `<=>` / `==` / `eql?` / `hash` defined here.
Instance Method Summary collapse
- #<=>(other) ⇒ -1, ...
- #==(other) ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Integer
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #same_type?(other) ⇒ Boolean
- #zero? ⇒ Boolean
Instance Method Details
#<=>(other) ⇒ -1, ...
43 44 45 46 47 48 49 50 |
# File 'lib/amount/comparison.rb', line 43 def <=>(other) return nil unless other.is_a?(Amount) comparable = coerce_other_to_self_type(other) return nil unless comparable @atomic <=> comparable.atomic end |
#==(other) ⇒ Boolean
57 58 59 |
# File 'lib/amount/comparison.rb', line 57 def ==(other) same_type?(other) && @atomic == other.atomic end |
#eql?(other) ⇒ Boolean
66 67 68 |
# File 'lib/amount/comparison.rb', line 66 def eql?(other) other.class == self.class && symbol == other.symbol && @atomic == other.atomic end |
#hash ⇒ Integer
74 75 76 |
# File 'lib/amount/comparison.rb', line 74 def hash [self.class, symbol, @atomic].hash end |
#negative? ⇒ Boolean
36 |
# File 'lib/amount/comparison.rb', line 36 def negative? = @atomic.negative? |
#positive? ⇒ Boolean
30 |
# File 'lib/amount/comparison.rb', line 30 def positive? = @atomic.positive? |
#same_type?(other) ⇒ Boolean
16 17 18 |
# File 'lib/amount/comparison.rb', line 16 def same_type?(other) other.is_a?(Amount) && other.symbol == symbol end |
#zero? ⇒ Boolean
24 |
# File 'lib/amount/comparison.rb', line 24 def zero? = @atomic.zero? |