From a96e824c5388ed756685a041578688a7e14a8247 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sat, 23 Jul 2011 15:38:55 -0400 Subject: Make Enumerable#many? not rely on #size --- activesupport/lib/active_support/core_ext/enumerable.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 3e05c6eaf2..9b835aab9e 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -93,10 +93,10 @@ module Enumerable 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 = block_given? ? count(&block) : to_a.size size > 1 end -- cgit v1.2.3 From c78503883902497521a710262a9ec005ca98ff74 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sat, 23 Jul 2011 15:40:58 -0400 Subject: Make Enumerable#many? iterate only over what is necessary --- activesupport/lib/active_support/core_ext/enumerable.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') 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. -- cgit v1.2.3 From 816abecf90167c4df7d2117188bfa9d19b752696 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sat, 23 Jul 2011 15:41:36 -0400 Subject: Insure that Enumerable#index_by, group_by, ... return Enumerators --- activesupport/lib/active_support/core_ext/enumerable.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index f67e7bf33e..f2dd34cc55 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| @@ -76,6 +77,7 @@ module Enumerable # (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1 # def each_with_object(memo, &block) + return to_enum :each_with_object, memo unless block_given? each do |element| block.call(element, memo) end @@ -90,6 +92,7 @@ module Enumerable # => { "Chade- Fowlersburg-e" => , "David Heinemeier Hansson" => , ...} # def index_by + return to_enum :index_by unless block_given? Hash[map { |elem| [yield(elem), elem] }] end -- cgit v1.2.3 From f061ffd9f4e7f7d612d4baa25e8075bb23896275 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sat, 23 Jul 2011 15:41:55 -0400 Subject: Trivial optimization for Enumerable#each_with_object --- activesupport/lib/active_support/core_ext/enumerable.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index f2dd34cc55..ddb4f3012f 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -76,10 +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) -- cgit v1.2.3