Skip to content

Commit

Permalink
Add Enumerable#exclude? to bring parity to Enumerable#include? and av…
Browse files Browse the repository at this point in the history
…oid if !x.include?/else calls [DHH]
  • Loading branch information
dhh committed Dec 15, 2009
1 parent 5f8e48c commit 7b61541
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge* *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 Edinburgh TimeZone to use "Europe/London" instead of "Europe/Dublin" #3310 [Phil Ross]


* Update bundled TZInfo to v0.3.15 [Geoff Buesing] * Update bundled TZInfo to v0.3.15 [Geoff Buesing]
Expand Down
5 changes: 5 additions & 0 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -101,6 +101,11 @@ def many?(&block)
size = block_given? ? select(&block).size : self.size size = block_given? ? select(&block).size : self.size
size > 1 size > 1
end end

# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
def exclude?(object)
!include?(object)
end
end end


class Range #:nodoc: class Range #:nodoc:
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/enumerable_test.rb
Expand Up @@ -89,4 +89,9 @@ def test_many
assert ![ 1, 2 ].many? {|x| x > 1 } assert ![ 1, 2 ].many? {|x| x > 1 }
assert [ 1, 2, 2 ].many? {|x| x > 1 } assert [ 1, 2, 2 ].many? {|x| x > 1 }
end end

def test_exclude?
assert [ 1 ].exclude?(2)
assert ![ 1 ].exclude?(1)
end
end end

8 comments on commit 7b61541

@robbyrussell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd wonder why this wasn't just part of Ruby. ;-)

@Bounga
Copy link
Contributor

@Bounga Bounga commented on 7b61541 Dec 15, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition

@duff
Copy link
Contributor

@duff duff commented on 7b61541 Dec 15, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dig it.

@spp
Copy link

@spp spp commented on 7b61541 Dec 15, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this popular, I was already using it for a while, after drawing inspiration from the whole Core Extension module of Rails.

@kamk
Copy link

@kamk kamk commented on 7b61541 Dec 15, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also like:

object.in?(collection)
object.not_in?(collection)

@dubek
Copy link
Contributor

@dubek dubek commented on 7b61541 Dec 16, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I disagree. On the same basis you should add invalid? to ActiveRecord, not_ssl? to the Request class etc. Ruby has a very short not operator (!) -- why not use it?

Yes, I know that ruby has "select" and "reject" which can be defined one using the other; still I think these are somewhat OK whereas "exclude?" (which is really "not_include?") is not needed.

@dtrasbo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dubek,

One of the major things that makes Ruby cool is readability, and the addition of Enumerable#exclude? certainly makes for more readability, and it does something we need all the time.

@rfunduk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me that unless x.include?( obj ) is actually more clear in this case... but no harm done.

Please sign in to comment.