Module: VectorNumber::Comparing
Overview
Methods for comparing with other numbers.
Comparable
is included for parity with numbers.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compare to
other
and return -1, 0, or 1 ifself
is less than, equal, or larger thanother
. -
#==(other) ⇒ Boolean
Compare to
other
for equality. -
#eql?(other) ⇒ Boolean
Compare to
other
for strict equality. -
#hash ⇒ Integer
Generate an Integer hash value for self.
Instance Method Details
#<=>(other) ⇒ Integer?
Compare to other
and return -1, 0, or 1 if self
is less than, equal, or larger than other
.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/vector_number/comparing.rb', line 112 def <=>(other) return nil unless numeric?(1) case other when VectorNumber other.numeric?(1) ? real <=> other.real : nil when Numeric other.imaginary.zero? ? real <=> other.real : nil else nil end end |
#==(other) ⇒ Boolean
Compare to other
for equality.
Values are considered equal if
-
other
is a VectorNumber and it iseql?
to this one, or -
other
is a Numeric equal in value to this (real or complex) number.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vector_number/comparing.rb', line 32 def ==(other) return true if eql?(other) case other when Numeric numeric?(2) && other.real == real && other.imaginary == imaginary else # Can't compare a number-like value to a non-number. false end end |
#eql?(other) ⇒ Boolean
Compare to other
for strict equality.
Values are considered equal only if other
is a VectorNumber and it has exactly the same units and coefficients, though possibly in a different order. Additionally, ‘a.eql?(b)` implies `a.hash == b.hash`.
Note that VectorNumber#options are not considered for equality.
63 64 65 66 67 68 69 70 71 |
# File 'lib/vector_number/comparing.rb', line 63 def eql?(other) return true if equal?(other) if other.is_a?(VectorNumber) other.size == size && other.each_pair.all? { |u, c| @data[u] == c } else false end end |
#hash ⇒ Integer
Generate an Integer hash value for self.
Hash values are stable during runtime, but not between processes.
85 86 87 |
# File 'lib/vector_number/comparing.rb', line 85 def hash [self.class, @data].hash end |