Module: Jinx::PartialOrder

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

Overview

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

  • a <=> b returns -1, 0, or 1 if a and b are comparable, nil otherwise

A PartialOrder thus relaxes comparison symmetry, e.g.

a < b

does not imply

b >= a.

Example:

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 a 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



31
32
33
# File 'lib/jinx/helpers/partial_order.rb', line 31

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