aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-06-20 11:31:06 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-06-20 11:31:06 -0500
commit161ab28b7c0bd28828b6d18613a9de09d47023c6 (patch)
treee66809696a44b505b2f1e379f54adbbf67930300 /activesupport
parent00ba4c0cf32f9417d47bd891eba97f2e04609520 (diff)
downloadrails-161ab28b7c0bd28828b6d18613a9de09d47023c6.tar.gz
rails-161ab28b7c0bd28828b6d18613a9de09d47023c6.tar.bz2
rails-161ab28b7c0bd28828b6d18613a9de09d47023c6.zip
Added block-handling to Enumerable#many? (Damian Janowski) [#452 state:resolved]
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb4
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb9
3 files changed, 11 insertions, 4 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index d87558aba9..4f61311d95 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -6,7 +6,7 @@
* Added Object#present? which is equivalent to !Object#blank? [DHH]
-* Added Enumberable#many? to encapsulate collection.size > 1 [DHH]
+* Added Enumberable#many? to encapsulate collection.size > 1 [DHH/Damian Janowski]
* Add more standard Hash methods to ActiveSupport::OrderedHash [Steve Purcell]
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 9647797ec2..e451e9933a 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -79,7 +79,9 @@ module Enumerable
end
# Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1.
- def many?
+ # Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26.
+ def many?(&block)
+ size = block_given? ? select(&block).size : self.size
size > 1
end
end
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index fb9b9b8916..2315d8f3db 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -63,10 +63,15 @@ class EnumerableTests < Test::Unit::TestCase
assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] },
payments.index_by { |p| p.price })
end
-
- def test_several
+
+ def test_many
assert ![].many?
assert ![ 1 ].many?
assert [ 1, 2 ].many?
+
+ assert ![].many? {|x| x > 1 }
+ assert ![ 2 ].many? {|x| x > 1 }
+ assert ![ 1, 2 ].many? {|x| x > 1 }
+ assert [ 1, 2, 2 ].many? {|x| x > 1 }
end
end