mirror of https://github.com/rails/rails
Make Enumerable#many? not rely on #size
This commit is contained in:
parent
f6ac022a6f
commit
a96e824c53
|
@ -93,10 +93,10 @@ module Enumerable
|
||||||
Hash[map { |elem| [yield(elem), elem] }]
|
Hash[map { |elem| [yield(elem), elem] }]
|
||||||
end
|
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.
|
# 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)
|
def many?(&block)
|
||||||
size = block_given? ? count(&block) : self.size
|
size = block_given? ? count(&block) : to_a.size
|
||||||
size > 1
|
size > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ class EnumerableTests < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_index_by
|
def test_index_by
|
||||||
payments = GenericEnumerable.new([ Payment.new(5), Payment.new(15), Payment.new(10) ])
|
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 })
|
payments.index_by { |p| p.price })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue