Created
September 21, 2011 06:50
-
-
Save unicornrainbow/1231424 to your computer and use it in GitHub Desktop.
A refined version of #in, #eq, #any_in, #all_in, and #none_in for object and enumerable.
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
| # all_in, any_in, and none_in extension to Array. | |
| # | |
| module Enumerable | |
| # Returns true if the argument list contain every item of the array. | |
| # | |
| # Basic Usage: | |
| # | |
| # [:black, :blue].all_in :black, :blue # => true | |
| # [:black].all_in :black, :red # => true | |
| # [:black, :blue].all_in :black, :red, :blue # => false | |
| # | |
| def all_in(*ary) | |
| reduce(true) { |memo, item| memo && item.in(*ary) } | |
| end | |
| # Returns true if the argument list contains one or more items from the array . | |
| # | |
| # Basic Usage: | |
| # | |
| # [:black, :blue].any_in :black # => true | |
| # [:black, :blue].any_in :black, :red # => true | |
| # [:black, :blue].any_in :green, :red # => false | |
| # | |
| def any_in(*ary) | |
| reduce(false) { |memo, item| memo || item.in(*ary) } | |
| end | |
| # Return true is the the argument list has no items in common with the array. (Opsitie of any_in) | |
| # | |
| # [:black, :blue].none_in :black # => false | |
| # [:black, :blue].none_in :black, :red # => false | |
| # [:black, :blue].none_in :green, :red # => true | |
| # | |
| def none_in(*ary) | |
| !any_in(*ary) | |
| end | |
| end |
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
| class Object | |
| # Returns true if the the argument list includes the object. | |
| # | |
| # Usage in conditionals: | |
| # | |
| # if 1.in 1, 2, 3 | |
| # puts "1 was included" | |
| # end | |
| # | |
| # if "x".in *xyz | |
| # puts "x was included" | |
| # end | |
| # | |
| # if color.in :white, :gray, :black | |
| # puts "#{color} isn’t a color." | |
| # end | |
| # | |
| # It can also be used as a safe alternative to ==. | |
| # | |
| # if 1.in 1.0 | |
| # puts "1 == 1.0 #=> true" | |
| # end | |
| # | |
| # Using in like this avoids accidental assignment, like .eql?, but unlike .eql? | |
| # has the same semantics as ==, which is often ideal. | |
| # | |
| def in(head, *tail) | |
| eq(head) || tail.any? && self.in(*tail) | |
| end | |
| # Use .eq where you would use ==. Avoid accedential assignment. | |
| # | |
| # if 1.eq 1 | |
| # puts "They're equal" | |
| # end | |
| # | |
| alias :eq :== | |
| # True if the argument list contains the object. | |
| # | |
| # Note: any_in and all_in on a single object is the same as #in. | |
| # | |
| # Basic Usage: | |
| # | |
| # :black.all_in :black # => true | |
| # :black.any_in :black # => true | |
| # :black.all_in :black, :red # => true | |
| # :black.any_in :black, :red # => true | |
| # :black.all_in :red, :blue # => false | |
| # :black.any_in :red, :blue # => false | |
| # | |
| alias :all_in :in | |
| alias :any_in :in | |
| # Returns false is the argument list contains the object. | |
| # | |
| def not_in(*ary) | |
| !self.in(*ary) | |
| end | |
| alias :none_in :not_in | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If Ruby is a language that tries to reduce the surprise, so why was I trying to write this so many times?
to see it doesn't work? I had to write things like this:
or
that does not seem as smart as other language sentences.
I am happy that CoffeeScript includes the "in" statement, and now I'm happy to see it is easy to include "in" in your project if you want to.
<3 <3 <3