Skip to content

Commit

Permalink
Added Enumerable#none? to check that none of the elements match the b…
Browse files Browse the repository at this point in the history
…lock [#1408 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
djanowski authored and dhh committed Nov 19, 2008
1 parent 917428b commit f451f0e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -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]
Expand Down
9 changes: 9 additions & 0 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -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 changes: 10 additions & 0 deletions activesupport/test/core_ext/enumerable_test.rb
Expand Up @@ -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

0 comments on commit f451f0e

Please sign in to comment.