Skip to content

Commit

Permalink
support Numeric#zero? and Numeric#nonzero?
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Nov 20, 2015
1 parent 0e721ef commit 40a9700
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 40a9700

Please sign in to comment.