mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
04ec65b87a
- zero? - nonzero? - positive? - negative?
80 lines
1.4 KiB
Ruby
80 lines
1.4 KiB
Ruby
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 <tt>zero?</tt> 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:
|
|
# int.allbits?(mask) -> true or false
|
|
#
|
|
# Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.
|
|
#
|
|
def allbits?(mask)
|
|
(self & mask) == mask
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# int.anybits?(mask) -> true or false
|
|
#
|
|
# Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.
|
|
#
|
|
def anybits?(mask)
|
|
(self & mask) != 0
|
|
end
|
|
|
|
##
|
|
# call-seq:
|
|
# int.nobits?(mask) -> true or false
|
|
#
|
|
# Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.
|
|
#
|
|
def nobits?(mask)
|
|
(self & mask) == 0
|
|
end
|
|
end
|