| 1 |
class Numeric |
| 2 |
## |
| 3 |
# call-seq: |
| 4 |
# zero? -> true or false |
| 5 |
# |
| 6 |
# Returns +true+ if +zero+ has a zero value, +false+ otherwise. |
| 7 |
# |
| 8 |
# Of the Core and Standard Library classes, |
| 9 |
# only Rational and Complex use this implementation. |
| 10 |
# |
| 11 |
def zero? |
| 12 |
self == 0 |
| 13 |
end |
| 14 |
|
| 15 |
## |
| 16 |
# call-seq: |
| 17 |
# nonzero? -> self or nil |
| 18 |
# |
| 19 |
# Returns +self+ if +self+ is not a zero value, +nil+ otherwise; |
| 20 |
# uses method <tt>zero?</tt> for the evaluation. |
| 21 |
# |
| 22 |
def nonzero? |
| 23 |
if self == 0 |
| 24 |
nil |
| 25 |
else |
| 26 |
self |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
## |
| 31 |
# call-seq: |
| 32 |
# positive? -> true or false |
| 33 |
# |
| 34 |
# Returns +true+ if +self+ is greater than 0, +false+ otherwise. |
| 35 |
# |
| 36 |
def positive? |
| 37 |
self > 0 |
| 38 |
end |
| 39 |
|
| 40 |
## |
| 41 |
# call-seq: |
| 42 |
# negative? -> true or false |
| 43 |
# |
| 44 |
# Returns +true+ if +self+ is less than 0, +false+ otherwise. |
| 45 |
# |
| 46 |
def negative? |
| 47 |
self < 0 |
| 48 |
end |
| 49 |
|
| 50 |
## |
| 51 |
# call-seq: |
| 52 |
# int.allbits?(mask) -> true or false |
| 53 |
# |
| 54 |
# Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1. |
| 55 |
# |
| 56 |
def allbits?(mask) |
| 57 |
(self & mask) == mask |
| 58 |
end |
| 59 |
|
| 60 |
## |
| 61 |
# call-seq: |
| 62 |
# int.anybits?(mask) -> true or false |
| 63 |
# |
| 64 |
# Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1. |
| 65 |
# |
| 66 |
def anybits?(mask) |
| 67 |
(self & mask) != 0 |
| 68 |
end |
| 69 |
|
| 70 |
## |
| 71 |
# call-seq: |
| 72 |
# int.nobits?(mask) -> true or false |
| 73 |
# |
| 74 |
# Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1. |
| 75 |
# |
| 76 |
def nobits?(mask) |
| 77 |
(self & mask) == 0 |
| 78 |
end |
| 79 |
end |
| 80 |
|
| 81 |
class Integer |
| 82 |
# call-seq: |
| 83 |
# ceildiv(other) -> integer |
| 84 |
# |
| 85 |
# Returns the result of division +self+ by +other+. The |
| 86 |
# result is rounded up to the nearest integer. |
| 87 |
# |
| 88 |
# 3.ceildiv(3) # => 1 |
| 89 |
# 4.ceildiv(3) # => 2 |
| 90 |
# |
| 91 |
# 4.ceildiv(-3) # => -1 |
| 92 |
# -4.ceildiv(3) # => -1 |
| 93 |
# -4.ceildiv(-3) # => 2 |
| 94 |
# |
| 95 |
# 3.ceildiv(1.2) # => 3 |
| 96 |
def ceildiv(other) |
| 97 |
-div(-other) |
| 98 |
end |
| 99 |
end |