125 lines
2.2 KiB
Ruby
Vendored
125 lines
2.2 KiB
Ruby
Vendored
class Numeric
|
|
##
|
|
# call-seq:
|
|
# zero? -> true or false
|
|
#
|
|
# Returns `true` if `zero` has a zero value, `false` otherwise.
|
|
#
|
|
# Of the Core and Standard Library classes,
|
|
# only Rational and Complex use this implementation.
|
|
#
|
|
def zero?
|
|
self == 0
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# nonzero? -> self or nil
|
|
#
|
|
# Returns `self` if `self` is not a zero value, `nil` otherwise;
|
|
# uses method `zero?` for the evaluation.
|
|
#
|
|
def nonzero?
|
|
if self == 0
|
|
nil
|
|
else
|
|
self
|
|
end
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# positive? -> true or false
|
|
#
|
|
# Returns `true` if `self` is greater than 0, `false` otherwise.
|
|
#
|
|
def positive?
|
|
self > 0
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# negative? -> true or false
|
|
#
|
|
# Returns `true` if `self` is less than 0, `false` otherwise.
|
|
#
|
|
def negative?
|
|
self < 0
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# num.integer? -> true or false
|
|
#
|
|
# Returns true if num is an Integer.
|
|
#
|
|
# 1.0.integer? #=> false
|
|
# 1.integer? #=> true
|
|
#
|
|
def integer?
|
|
false
|
|
end
|
|
end
|
|
|
|
class Integer
|
|
##
|
|
# call-seq:
|
|
# int.allbits?(mask) -> true or false
|
|
#
|
|
# Returns `true` if all bits of ``int` & `mask`` are 1.
|
|
#
|
|
def allbits?(mask)
|
|
(self & mask) == mask
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# int.anybits?(mask) -> true or false
|
|
#
|
|
# Returns `true` if any bits of ``int` & `mask`` are 1.
|
|
#
|
|
def anybits?(mask)
|
|
(self & mask) != 0
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# int.nobits?(mask) -> true or false
|
|
#
|
|
# Returns `true` if no bits of ``int` & `mask`` are 1.
|
|
#
|
|
def nobits?(mask)
|
|
(self & mask) == 0
|
|
end
|
|
|
|
# call-seq:
|
|
# ceildiv(other) -> integer
|
|
#
|
|
# Returns the result of division `self` by `other`. The
|
|
# result is rounded up to the nearest integer.
|
|
#
|
|
# 3.ceildiv(3) # => 1
|
|
# 4.ceildiv(3) # => 2
|
|
#
|
|
# 4.ceildiv(-3) # => -1
|
|
# -4.ceildiv(3) # => -1
|
|
# -4.ceildiv(-3) # => 2
|
|
#
|
|
# 3.ceildiv(1.2) # => 3
|
|
def ceildiv(other)
|
|
-div(-other)
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# int.integer? -> true
|
|
#
|
|
# Returns true since this is an Integer.
|
|
#
|
|
# 1.integer? #=> true
|
|
# 42.integer? #=> true
|
|
#
|
|
def integer?
|
|
true
|
|
end
|
|
end
|