Module: Jinx::PartialOrder

Includes:
Comparable
Defined in:
lib/jinx/helpers/partial_order.rb

Overview

A PartialOrder is a Comparable with restricted scope. Classes which include PartialOrder are required to implement the <=> operator with the following semantics:

  • If if a and b are comparable, then return the value of the comparison.

  • Otherwise, return nil.

A PartialOrder thus relaxes comparison symmetry, e.g.:

a < b

does not imply:

b >= a.

Examples:

module Queued
  attr_reader :queue
  def <=>(other)
    queue.index(self) <=> queue.index(other) if queue.equal?(other.queue)
  end
end
q1 = [a, b] # a, b are Queued
q2 = [c]    # c is Queued
a < b #=> true
b < c #=> nil

Instance Method Summary collapse

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns true if other is an instance of this object’s class and other == self, false otherwise.

Returns:

  • (Boolean)

    true if other is an instance of this object’s class and other == self, false otherwise



39
40
41
# File 'lib/jinx/helpers/partial_order.rb', line 39

def eql?(other)
  self.class === other and super
end