aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-06-21 07:00:28 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-06-21 07:00:28 +0000
commit6c8d354b8d9ca2fa62d0e062fa6346673dd15dbf (patch)
tree314efa0a4540a27b4218a4bdc3cf01e1b35dc537
parentb69199c1da3ef89bdbebb98084bcf6fe31732a53 (diff)
downloadrails-6c8d354b8d9ca2fa62d0e062fa6346673dd15dbf.tar.gz
rails-6c8d354b8d9ca2fa62d0e062fa6346673dd15dbf.tar.bz2
rails-6c8d354b8d9ca2fa62d0e062fa6346673dd15dbf.zip
Added Fix/Bignum#multiple_of? which returns true on 14.multiple_of?(7) and false on 16.multiple_of?(7) #1464 [Thomas Fuchs] Added even? and odd? to work with Bignums in addition to Fixnums #1464 [Thomas Fuchs]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1462 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/lib/active_support/core_ext/fixnum.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/fixnum/even_odd.rb6
-rw-r--r--activesupport/test/core_ext/fixnum_ext_test.rb15
3 files changed, 24 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/fixnum.rb b/activesupport/lib/active_support/core_ext/fixnum.rb
index fecef1da52..e983d5c0f9 100644
--- a/activesupport/lib/active_support/core_ext/fixnum.rb
+++ b/activesupport/lib/active_support/core_ext/fixnum.rb
@@ -3,3 +3,7 @@ require File.dirname(__FILE__) + '/fixnum/even_odd'
class Fixnum #:nodoc:
include ActiveSupport::CoreExtensions::Fixnum::EvenOdd
end
+
+class Bignum #:nodoc:
+ include ActiveSupport::CoreExtensions::Fixnum::EvenOdd
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb b/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb
index 1fa6b95846..c0e9da18fa 100644
--- a/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb
+++ b/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb
@@ -7,8 +7,12 @@ module ActiveSupport #:nodoc:
# * 2.even? # => true
# * 2.odd? # => false
module EvenOdd
+ def multiple_of?(number)
+ self % number == 0
+ end
+
def even?
- self % 2 == 0
+ multiple_of? 2
end
def odd?
diff --git a/activesupport/test/core_ext/fixnum_ext_test.rb b/activesupport/test/core_ext/fixnum_ext_test.rb
index 627c282b8b..7ffc32a9e1 100644
--- a/activesupport/test/core_ext/fixnum_ext_test.rb
+++ b/activesupport/test/core_ext/fixnum_ext_test.rb
@@ -5,10 +5,25 @@ class FixnumExtTest < Test::Unit::TestCase
def test_even
assert [ -2, 0, 2, 4 ].all? { |i| i.even? }
assert ![ -1, 1, 3 ].all? { |i| i.even? }
+
+ assert 22953686867719691230002707821868552601124472329079.odd?
+ assert !22953686867719691230002707821868552601124472329079.even?
+ assert 22953686867719691230002707821868552601124472329080.even?
+ assert !22953686867719691230002707821868552601124472329080.odd?
end
def test_odd
assert ![ -2, 0, 2, 4 ].all? { |i| i.odd? }
assert [ -1, 1, 3 ].all? { |i| i.odd? }
end
+
+ def test_multiple_of
+ assert [ -7, 0, 7, 14 ].all? { |i| i.multiple_of? 7 }
+ assert ![ -7, 0, 7, 14 ].all? { |i| i.multiple_of? 6 }
+ # test with a prime
+ assert !22953686867719691230002707821868552601124472329079.multiple_of?(2)
+ assert !22953686867719691230002707821868552601124472329079.multiple_of?(3)
+ assert !22953686867719691230002707821868552601124472329079.multiple_of?(5)
+ assert !22953686867719691230002707821868552601124472329079.multiple_of?(7)
+ end
end