mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Added block-handling to Enumerable#many? (Damian Janowski) [#452 state:resolved]
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
* Added Object#present? which is equivalent to !Object#blank? [DHH]
|
||||
|
||||
* Added Enumberable#many? to encapsulate collection.size > 1 [DHH]
|
||||
* Added Enumberable#many? to encapsulate collection.size > 1 [DHH/Damian Janowski]
|
||||
|
||||
* Add more standard Hash methods to ActiveSupport::OrderedHash [Steve Purcell]
|
||||
|
||||
|
||||
@@ -79,7 +79,9 @@ module Enumerable
|
||||
end
|
||||
|
||||
# Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1.
|
||||
def many?
|
||||
# Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26.
|
||||
def many?(&block)
|
||||
size = block_given? ? select(&block).size : self.size
|
||||
size > 1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -63,10 +63,15 @@ class EnumerableTests < Test::Unit::TestCase
|
||||
assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] },
|
||||
payments.index_by { |p| p.price })
|
||||
end
|
||||
|
||||
def test_several
|
||||
|
||||
def test_many
|
||||
assert ![].many?
|
||||
assert ![ 1 ].many?
|
||||
assert [ 1, 2 ].many?
|
||||
|
||||
assert ![].many? {|x| x > 1 }
|
||||
assert ![ 2 ].many? {|x| x > 1 }
|
||||
assert ![ 1, 2 ].many? {|x| x > 1 }
|
||||
assert [ 1, 2, 2 ].many? {|x| x > 1 }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user