diff options
author | Marc-Andre Lafortune <github@marc-andre.ca> | 2011-07-23 15:40:58 -0400 |
---|---|---|
committer | Marc-Andre Lafortune <github@marc-andre.ca> | 2011-07-23 15:52:37 -0400 |
commit | c78503883902497521a710262a9ec005ca98ff74 (patch) | |
tree | 3948de04278a1d08f88010a3d4ba65feb122c6c2 /activesupport/lib/active_support/core_ext/enumerable.rb | |
parent | a96e824c5388ed756685a041578688a7e14a8247 (diff) | |
download | rails-c78503883902497521a710262a9ec005ca98ff74.tar.gz rails-c78503883902497521a710262a9ec005ca98ff74.tar.bz2 rails-c78503883902497521a710262a9ec005ca98ff74.zip |
Make Enumerable#many? iterate only over what is necessary
Diffstat (limited to 'activesupport/lib/active_support/core_ext/enumerable.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 9b835aab9e..f67e7bf33e 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -95,9 +95,16 @@ module Enumerable # Returns true if the enumerable has more than 1 element. Functionally equivalent to enum.to_a.size > 1. # Can be called with a block too, much like any?, so people.many? { |p| p.age > 26 } returns true if more than 1 person is over 26. - def many?(&block) - size = block_given? ? count(&block) : to_a.size - size > 1 + def many? + cnt = 0 + if block_given? + any? do |element| + cnt += 1 if yield element + cnt > 1 + end + else + any?{ (cnt += 1) > 1 } + end end # The negative of the Enumerable#include?. Returns true if the collection does not include the object. |