public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added Enumerable#none? to check that none of the elements match the block [#1408 
state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
djanowski (author)
Tue Nov 18 12:53:06 -0800 2008
dhh (committer)
Wed Nov 19 01:48:41 -0800 2008
commit  f451f0e5cfa358e88ac9d03d813a9c84facd6648
tree    1b5f2831b934d92f21a8f5ab16ed536ee5e90dc8
parent  917428bcce7bb22241bfc07daa5d0ddf9d107775
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *2.3.0/3.0*
0
 
0
+* Added Enumerable#none? to check that none of the elements match the block #1408 [Damian Janowski]
0
+
0
 * TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing]
0
 
0
 * Update bundled TZInfo to 0.3.12 [Geoff Buesing]
...
104
105
106
 
 
 
 
 
 
 
 
 
107
...
104
105
106
107
108
109
110
111
112
113
114
115
116
0
@@ -104,4 +104,13 @@ module Enumerable
0
     size = block_given? ? select(&block).size : self.size
0
     size > 1
0
   end
0
+
0
+  # Returns true if none of the elements match the given block.
0
+  # 
0
+  #   success = responses.none? {|r| r.status / 100 == 3 }
0
+  #
0
+  def none?(&block)
0
+    return true if !block_given? || blank?
0
+    !any?(&block)
0
+  end
0
 end
...
79
80
81
 
 
 
 
 
 
 
 
 
 
82
...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
0
@@ -79,4 +79,14 @@ class EnumerableTests < Test::Unit::TestCase
0
     assert ![ 1, 2 ].many? {|x| x > 1 }
0
     assert [ 1, 2, 2 ].many? {|x| x > 1 }
0
   end
0
+
0
+  def test_none
0
+    assert [].none?
0
+    assert [ 1 ].none?
0
+
0
+    assert [].none? {|x| x > 1 }
0
+    assert ![ 2 ].none? {|x| x > 1 }
0
+    assert ![ 1, 2 ].none? {|x| x > 1 }
0
+    assert [ 1, 1 ].none? {|x| x > 1 }
0
+  end
0
 end

Comments