Make Enumerable#many? not rely on #size

This commit is contained in:
Marc-Andre Lafortune 2011-07-23 15:38:55 -04:00
parent f6ac022a6f
commit a96e824c53
2 changed files with 3 additions and 3 deletions

View File

@ -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

View File

@ -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