Skip to content
This repository
Browse code

Added Enumerable#none? to check that none of the elements match the b…

…lock [#1408 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information...
commit f451f0e5cfa358e88ac9d03d813a9c84facd6648 1 parent 917428b
Damian Janowski authored November 18, 2008 dhh committed November 19, 2008
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.3.0/3.0*
2 2
 
  3
+* Added Enumerable#none? to check that none of the elements match the block #1408 [Damian Janowski]
  4
+
3 5
 * TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing]
4 6
 
5 7
 * Update bundled TZInfo to 0.3.12 [Geoff Buesing]
9  activesupport/lib/active_support/core_ext/enumerable.rb
@@ -104,4 +104,13 @@ def many?(&block)
104 104
     size = block_given? ? select(&block).size : self.size
105 105
     size > 1
106 106
   end
  107
+
  108
+  # Returns true if none of the elements match the given block.
  109
+  # 
  110
+  #   success = responses.none? {|r| r.status / 100 == 3 }
  111
+  #
  112
+  def none?(&block)
  113
+    return true if !block_given? || blank?
  114
+    !any?(&block)
  115
+  end
107 116
 end
10  activesupport/test/core_ext/enumerable_test.rb
@@ -79,4 +79,14 @@ def test_many
79 79
     assert ![ 1, 2 ].many? {|x| x > 1 }
80 80
     assert [ 1, 2, 2 ].many? {|x| x > 1 }
81 81
   end
  82
+
  83
+  def test_none
  84
+    assert [].none?
  85
+    assert [ 1 ].none?
  86
+
  87
+    assert [].none? {|x| x > 1 }
  88
+    assert ![ 2 ].none? {|x| x > 1 }
  89
+    assert ![ 1, 2 ].none? {|x| x > 1 }
  90
+    assert [ 1, 1 ].none? {|x| x > 1 }
  91
+  end
82 92
 end

0 notes on commit f451f0e

Please sign in to comment.
Something went wrong with that request. Please try again.