Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 djanowski authored dhh committed
2  activesupport/CHANGELOG
View
@@ -1,5 +1,7 @@
*2.3.0/3.0*
+* Added Enumerable#none? to check that none of the elements match the block #1408 [Damian Janowski]
+
* TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing]
* Update bundled TZInfo to 0.3.12 [Geoff Buesing]
9 activesupport/lib/active_support/core_ext/enumerable.rb
View
@@ -104,4 +104,13 @@ def many?(&block)
size = block_given? ? select(&block).size : self.size
size > 1
end
+
+ # Returns true if none of the elements match the given block.
+ #
+ # success = responses.none? {|r| r.status / 100 == 3 }
+ #
+ def none?(&block)
+ return true if !block_given? || blank?
+ !any?(&block)
+ end
end
10 activesupport/test/core_ext/enumerable_test.rb
View
@@ -79,4 +79,14 @@ def test_many
assert ![ 1, 2 ].many? {|x| x > 1 }
assert [ 1, 2, 2 ].many? {|x| x > 1 }
end
+
+ def test_none
+ assert [].none?
+ assert [ 1 ].none?
+
+ assert [].none? {|x| x > 1 }
+ assert ![ 2 ].none? {|x| x > 1 }
+ assert ![ 1, 2 ].none? {|x| x > 1 }
+ assert [ 1, 1 ].none? {|x| x > 1 }
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.