W3cubDocs

/Crystal 0.31

module PartialComparable(T)

Overview

The PartialComparable mixin is used by classes whose objects may be partially ordered.

Including types must provide an #<=> method, which compares the receiver against another object, returning a negative number, 0, a positive number or nil depending on whether the receiver is less than, equal to, greater than the other object, or no order can be established.

PartialComparable uses #<=> to implement the conventional comparison operators (#<, #<=, #==, #>=, and #>).

DEPRECATED This module is deprecated as of Crystal 0.28.0. Its behaviour has been fully integrated into Comparable.

Defined in:

partial_comparable.cr

Instance Method Summary

  • #<(other : T)

    Compares this object to other based on the receiver's #<=> method, returning true if it returns a negative number.

  • #<=(other : T)

    Compares this object to other based on the receiver's #<=> method, returning true if it returns a negative number or 0.

  • #<=>(other : T)

    The comparison operator.

  • #==(other : T)

    Compares this object to other based on the receiver's #<=> method, returning true if it returns 0.

  • #>(other : T)

    Compares this object to other based on the receiver's #<=> method, returning true if it returns a positive number.

  • #>=(other : T)

    Compares this object to other based on the receiver's #<=> method, returning true if it returns a positive number or 0.

  • #compare_with(other : T, &block)

Instance Method Detail

def <(other : T)Source

Compares this object to other based on the receiver's #<=> method, returning true if it returns a negative number.

def <=(other : T)Source

Compares this object to other based on the receiver's #<=> method, returning true if it returns a negative number or 0.

abstract def <=>(other : T)Source

The comparison operator.

Returns a negative number, 0, a positive number or nil depending on whether the object is considered to be less than other, equal to other, greater than other or if no order can be established.

Subclasses define this method to provide class-specific ordering.

# Sort in a descending way
[4, 7, 2].sort { |x, y| y <=> x } # => [7, 4, 2]

def ==(other : T)Source

Compares this object to other based on the receiver's #<=> method, returning true if it returns 0. Also returns true if this and other are the same object.

def >(other : T)Source

Compares this object to other based on the receiver's #<=> method, returning true if it returns a positive number.

def >=(other : T)Source

Compares this object to other based on the receiver's #<=> method, returning true if it returns a positive number or 0.

def compare_with(other : T, &block)Source

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/PartialComparable.html