Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Enumerable#exclude? to bring parity to Enumerable#include? and av…
…oid if !x.include?/else calls [DHH]
- Loading branch information
Showing
3 changed files
with
12 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7b61541
There was a problem hiding this comment.
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. ;-)
7b61541
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great addition
7b61541
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dig it.
7b61541
There was a problem hiding this comment.
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.
7b61541
There was a problem hiding this comment.
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)
7b61541
There was a problem hiding this comment.
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.
7b61541
There was a problem hiding this comment.
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.
7b61541
There was a problem hiding this comment.
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.