diff options
author | Marc-Andre Lafortune <github@marc-andre.ca> | 2011-07-23 15:38:55 -0400 |
---|---|---|
committer | Marc-Andre Lafortune <github@marc-andre.ca> | 2011-07-23 15:52:37 -0400 |
commit | a96e824c5388ed756685a041578688a7e14a8247 (patch) | |
tree | d3c4126222faf86fad8d479a0edd41072d075898 /activesupport | |
parent | f6ac022a6fa7ac68106ef8a85f2ce9b2465ef6e2 (diff) | |
download | rails-a96e824c5388ed756685a041578688a7e14a8247.tar.gz rails-a96e824c5388ed756685a041578688a7e14a8247.tar.bz2 rails-a96e824c5388ed756685a041578688a7e14a8247.zip |
Make Enumerable#many? not rely on #size
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 2 |
2 files changed, 3 insertions, 3 deletions
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 diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 9720b06f65..688207c91c 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -89,7 +89,7 @@ class EnumerableTests < Test::Unit::TestCase def test_index_by payments = GenericEnumerable.new([ Payment.new(5), Payment.new(15), Payment.new(10) ]) - assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] }, + assert_equal({ 5 => Payment.new(5), 15 => Payment.new(15), 10 => Payment.new(10) }, payments.index_by { |p| p.price }) end |