module Comparable[T]

The comparison operators, between? and clamp, for a class that includes it and defines <=>. T is the type compared with, usually the class itself (include Comparable[Version]).

class DocVersion_Comparable
  include Comparable[DocVersion_Comparable]
  @n: Int
  attr_reader :n
  def initialize(n: Int)
    @n = n
  end
  def <=>(other: DocVersion_Comparable) -> Int = n <=> other.n
end
DocVersion_Comparable.new(1) < DocVersion_Comparable.new(2)   # => true

<prelude>:224

Methods

def <=>(other: T) -> Int

Compares with other: negative when the receiver is less, zero when equal, positive when greater. The class including Comparable defines it.

<prelude>:228

def <(other: T) -> Bool

Whether the receiver is less than other.

<prelude>:231

def <=(other: T) -> Bool

Whether the receiver is less than or equal to other.

<prelude>:233

def >(other: T) -> Bool

Whether the receiver is greater than other.

<prelude>:235

def >=(other: T) -> Bool

Whether the receiver is greater than or equal to other.

<prelude>:237

def between?(min: T, max: T) -> Bool

Whether the receiver is in min..max, both ends included.

<prelude>:239

def clamp(min: T, max: T) -> Self

The receiver if it is between min and max, otherwise the nearer bound. Unlike Ruby, min greater than max is not an error: a receiver below min gives min, otherwise one above max gives max.

5.clamp(1, 3)   # => 3
0.clamp(1, 3)   # => 1
2.clamp(1, 3)   # => 2

<prelude>:251

def clamp(range: Range[T]) -> Self

The receiver limited to range, whose ends may be open (1.., ..3).

Raises

ArgumentError for an exclusive range (1...3).

<prelude>:258