diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 4edeadf10c0d7..cc4a2ff90ecee 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Add Enumerable#exclude? to bring parity to Enumerable#include? and avoid if !x.include?/else calls [DHH] + * Update Edinburgh TimeZone to use "Europe/London" instead of "Europe/Dublin" #3310 [Phil Ross] * Update bundled TZInfo to v0.3.15 [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index b11c916f61930..d0821a7c68177 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -101,6 +101,11 @@ def many?(&block) size = block_given? ? select(&block).size : self.size size > 1 end + + # The negative of the Enumerable#include?. Returns true if the collection does not include the object. + def exclude?(object) + !include?(object) + end end class Range #:nodoc: diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 66f5f9fbdec5a..4650b796b626b 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -89,4 +89,9 @@ def test_many assert ![ 1, 2 ].many? {|x| x > 1 } assert [ 1, 2, 2 ].many? {|x| x > 1 } end + + def test_exclude? + assert [ 1 ].exclude?(2) + assert ![ 1 ].exclude?(1) + end end