Skip to content

Instantly share code, notes, and snippets.

@Roman2K
Created May 21, 2011 12:32
Show Gist options
  • Save Roman2K/984482 to your computer and use it in GitHub Desktop.
Save Roman2K/984482 to your computer and use it in GitHub Desktop.
Integer#multiple_of?
class Numeric
def multiple_of?(*numbers)
zero? || numbers.all? { |n| n.nonzero? && modulo(n).zero? }
end
end
if $0 == __FILE__
require 'test/unit'
class MultipleOfTest < Test::Unit::TestCase
def test_multiple_of
assert 0.multiple_of?(1)
assert 0.0.multiple_of?(1)
assert 0.multiple_of?(2)
assert 0.multiple_of?(0)
assert !0.1.multiple_of?(0)
assert 1.multiple_of?(1)
assert 1.0.multiple_of?(1)
assert !1.multiple_of?(0)
assert !1.multiple_of?(2)
assert 2.multiple_of?(1, 2)
assert !2.multiple_of?(1, 2, 3)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment