From aeb9fedf918f3bc560f9f31d907d7db0822c9f3b Mon Sep 17 00:00:00 2001 From: Damian Janowski Date: Tue, 18 Nov 2008 18:53:06 -0200 Subject: [PATCH] Enumerable#none? checks that none of the elements match the block. --- .../lib/active_support/core_ext/enumerable.rb | 9 +++++++++ activesupport/test/core_ext/enumerable_test.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 788f3a7..12659d7 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -104,4 +104,13 @@ module Enumerable 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 diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index deb9b75..288ce14 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -79,4 +79,14 @@ class EnumerableTests < Test::Unit::TestCase 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 -- 1.6.0.4