diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-07-23 13:05:12 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-07-23 13:05:12 -0700 |
commit | abe61054af9aaceb546adfd629c50a027862a521 (patch) | |
tree | effc9947280a8c17c83985bae6eff9bebaa686d0 /activesupport/lib | |
parent | 2115068476e49b1e377d97e0c00e6d98263543d5 (diff) | |
parent | f061ffd9f4e7f7d612d4baa25e8075bb23896275 (diff) | |
download | rails-abe61054af9aaceb546adfd629c50a027862a521.tar.gz rails-abe61054af9aaceb546adfd629c50a027862a521.tar.bz2 rails-abe61054af9aaceb546adfd629c50a027862a521.zip |
Merge pull request #2217 from marcandre/fix_enumerable
Fix enumerable
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 3e05c6eaf2..ddb4f3012f 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -20,6 +20,7 @@ module Enumerable # "2006-02-24 -> Transcript, Transcript" # "2006-02-23 -> Transcript" def group_by + return to_enum :group_by unless block_given? assoc = ActiveSupport::OrderedHash.new each do |element| @@ -75,9 +76,10 @@ module Enumerable # # (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1 # - def each_with_object(memo, &block) + def each_with_object(memo) + return to_enum :each_with_object, memo unless block_given? each do |element| - block.call(element, memo) + yield element, memo end memo end unless [].respond_to?(:each_with_object) @@ -90,14 +92,22 @@ module Enumerable # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...} # def index_by + return to_enum :index_by unless block_given? Hash[map { |elem| [yield(elem), elem] }] end - # Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1. + # 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) : self.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. |