Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make Enumerable#many? not rely on #size

  • Loading branch information...
commit a96e824c5388ed756685a041578688a7e14a8247 1 parent f6ac022
@marcandre marcandre authored
View
4 activesupport/lib/active_support/core_ext/enumerable.rb
@@ -93,10 +93,10 @@ def index_by
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
2  activesupport/test/core_ext/enumerable_test.rb
@@ -89,7 +89,7 @@ def test_each_with_object
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
Please sign in to comment.
Something went wrong with that request. Please try again.