Skip to content

Commit

Permalink
Merge pull request #3030 from takahashim/nonzero
Browse files Browse the repository at this point in the history
Support Numeric#zero? and Numeric#nonzero?
  • Loading branch information
matz committed Nov 20, 2015
2 parents 0e721ef + 40a9700 commit 18190fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb
Expand Up @@ -2,4 +2,16 @@ module Integral
def div(other)
self.divmod(other)[0]
end

def zero?
self == 0
end

def nonzero?
if self == 0
nil
else
self
end
end
end
10 changes: 10 additions & 0 deletions mrbgems/mruby-numeric-ext/test/numeric.rb
Expand Up @@ -16,3 +16,13 @@
assert('Float#div') do
assert_float 52, 365.2425.div(7)
end

assert('Integer#zero?') do
assert_equal true, 0.zero?
assert_equal false, 1.zero?
end

assert('Integer#nonzero?') do
assert_equal nil, 0.nonzero?
assert_equal 1000, 1000.nonzero?
end

0 comments on commit 18190fa

Please sign in to comment.