Created
September 21, 2011 05:46
-
-
Save unicornrainbow/1231343 to your computer and use it in GitHub Desktop.
has_each, has_any, and has_none extension to Array.
This file contains hidden or 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
| # has_each, has_any, and has_none extension to Array. | |
| # | |
| class Array | |
| # Returns true is array contains each item in the argument list. | |
| # | |
| # Basic Usage: | |
| # | |
| # [:black, :blue].has_each :black # => true | |
| # [:black, :blue].has_each :black, :red # => false | |
| # [:black, :blue].has_each :black, :red, :blue # => true | |
| # | |
| def has_each(*ary) | |
| ary.reduce(true) { |memo, obj| memo && self.include?(object) } | |
| end | |
| alias :has_all :has_each | |
| # Returns true is array contains one or more items from the argument list. | |
| # | |
| # Basic Usage: | |
| # | |
| # [:black, :blue].has_any :black # => true | |
| # [:black, :blue].has_any :black, :red # => true | |
| # [:black, :blue].has_any :green, :red # => false | |
| # | |
| def has_any(*ary) | |
| ary.reduce { |memo, obj| memo || self.include?(object) } | |
| end | |
| # Return true is the ary hash no items in common with the argument list. (Opsitie of has_any) | |
| # | |
| # [:black, :blue].has_any :black # => false | |
| # [:black, :blue].has_any :black, :red # => false | |
| # [:black, :blue].has_any :green, :red # => true | |
| # | |
| def has_none(*ary) | |
| !has_any(*ary) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment