public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added block-handling to Enumerable#many? (Damian Janowski) [#452 
state:resolved]
dhh (author)
Fri Jun 20 09:31:06 -0700 2008
commit  161ab28b7c0bd28828b6d18613a9de09d47023c6
tree    e66809696a44b505b2f1e379f54adbbf67930300
parent  00ba4c0cf32f9417d47bd891eba97f2e04609520
...
6
7
8
9
 
10
11
12
...
6
7
8
 
9
10
11
12
0
@@ -6,7 +6,7 @@
0
 
0
 * Added Object#present? which is equivalent to !Object#blank? [DHH]
0
 
0
-* Added Enumberable#many? to encapsulate collection.size > 1 [DHH]
0
+* Added Enumberable#many? to encapsulate collection.size > 1 [DHH/Damian Janowski]
0
 
0
 * Add more standard Hash methods to ActiveSupport::OrderedHash [Steve Purcell]
0
 
...
79
80
81
82
 
 
 
83
84
85
...
79
80
81
 
82
83
84
85
86
87
0
@@ -79,7 +79,9 @@ module Enumerable
0
   end
0
   
0
   # Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1.
0
- def many?
0
+ # Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26.
0
+ def many?(&block)
0
+ size = block_given? ? select(&block).size : self.size
0
     size > 1
0
   end
0
 end
...
63
64
65
66
67
 
 
68
69
70
 
 
 
 
 
71
72
...
63
64
65
 
 
66
67
68
69
70
71
72
73
74
75
76
77
0
@@ -63,10 +63,15 @@ class EnumerableTests < Test::Unit::TestCase
0
     assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] },
0
                  payments.index_by { |p| p.price })
0
   end
0
-
0
- def test_several
0
+
0
+ def test_many
0
     assert ![].many?
0
     assert ![ 1 ].many?
0
     assert [ 1, 2 ].many?
0
+
0
+ assert ![].many? {|x| x > 1 }
0
+ assert ![ 2 ].many? {|x| x > 1 }
0
+ assert ![ 1, 2 ].many? {|x| x > 1 }
0
+ assert [ 1, 2, 2 ].many? {|x| x > 1 }
0
   end
0
 end

Comments

  • masterkain Fri Jun 20 10:05:32 -0700 2008

    small but nice change