| 1 |
## |
| 2 |
# Comparable |
| 3 |
# |
| 4 |
# ISO 15.3.3 |
| 5 |
module Comparable |
| 6 |
|
| 7 |
## |
| 8 |
# call-seq: |
| 9 |
# obj < other -> true or false |
| 10 |
# |
| 11 |
# Return true if +self+ is less |
| 12 |
# than +other+. Otherwise return |
| 13 |
# false. |
| 14 |
# |
| 15 |
# ISO 15.3.3.2.1 |
| 16 |
def < other |
| 17 |
cmp = self <=> other |
| 18 |
if cmp.nil? |
| 19 |
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" |
| 20 |
end |
| 21 |
cmp < 0 |
| 22 |
end |
| 23 |
|
| 24 |
## |
| 25 |
# call-seq: |
| 26 |
# obj <= other -> true or false |
| 27 |
# |
| 28 |
# Return true if +self+ is less |
| 29 |
# than or equal to +other+. |
| 30 |
# Otherwise return false. |
| 31 |
# |
| 32 |
# ISO 15.3.3.2.2 |
| 33 |
def <= other |
| 34 |
cmp = self <=> other |
| 35 |
if cmp.nil? |
| 36 |
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" |
| 37 |
end |
| 38 |
cmp <= 0 |
| 39 |
end |
| 40 |
|
| 41 |
## |
| 42 |
# call-seq: |
| 43 |
# obj == other -> true or false |
| 44 |
# |
| 45 |
# Return true if +self+ is equal |
| 46 |
# to +other+. Otherwise return |
| 47 |
# false. |
| 48 |
# |
| 49 |
# ISO 15.3.3.2.3 |
| 50 |
def == other |
| 51 |
cmp = self <=> other |
| 52 |
cmp.equal?(0) |
| 53 |
end |
| 54 |
|
| 55 |
## |
| 56 |
# call-seq: |
| 57 |
# obj > other -> true or false |
| 58 |
# |
| 59 |
# Return true if +self+ is greater |
| 60 |
# than +other+. Otherwise return |
| 61 |
# false. |
| 62 |
# |
| 63 |
# ISO 15.3.3.2.4 |
| 64 |
def > other |
| 65 |
cmp = self <=> other |
| 66 |
if cmp.nil? |
| 67 |
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" |
| 68 |
end |
| 69 |
cmp > 0 |
| 70 |
end |
| 71 |
|
| 72 |
## |
| 73 |
# call-seq: |
| 74 |
# obj >= other -> true or false |
| 75 |
# |
| 76 |
# Return true if +self+ is greater |
| 77 |
# than or equal to +other+. |
| 78 |
# Otherwise return false. |
| 79 |
# |
| 80 |
# ISO 15.3.3.2.5 |
| 81 |
def >= other |
| 82 |
cmp = self <=> other |
| 83 |
if cmp.nil? |
| 84 |
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" |
| 85 |
end |
| 86 |
cmp >= 0 |
| 87 |
end |
| 88 |
|
| 89 |
## |
| 90 |
# call-seq: |
| 91 |
# obj.between?(min,max) -> true or false |
| 92 |
# |
| 93 |
# Return true if +self+ is greater |
| 94 |
# than or equal to +min+ and |
| 95 |
# less than or equal to +max+. |
| 96 |
# Otherwise return false. |
| 97 |
# |
| 98 |
# ISO 15.3.3.2.6 |
| 99 |
def between?(min, max) |
| 100 |
self >= min and self <= max |
| 101 |
end |
| 102 |
end |