Module: VectorNumber::NumericRefinements::CommutativeShuttle

Defined in:
lib/vector_number/numeric_refinements.rb

Overview

Note:

Currently only applies to Complex on 3.1, as other numeric classes rely on #coerce.

Refinement module to provide a #<=> method that can work backwards.

Examples:

without refinements

VectorNumber[2] <=> Complex(1, 0) #=> 1
Complex(1, 0) <=> VectorNumber[2] #=> nil

with refinements

require "vector_number/numeric_refinements"
using VectorNumber::NumericRefinements
VectorNumber[2] <=> Complex(1, 0) #=> 1
Complex(1, 0) <=> VectorNumber[2] #=> -1

Since:

  • 0.2.1

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object

Commutative #<=>. Tries to call other <=> self if self <=> other returns nil.

Since:

  • 0.2.1



36
37
38
39
40
41
42
# File 'lib/vector_number/numeric_refinements.rb', line 36

def <=>(other)
  comparison = super
  return comparison if comparison || !other.respond_to?(:<=>)

  comparison = other <=> self
  -comparison if comparison
end