Created
August 25, 2011 12:17
-
-
Save zlw/1170527 to your computer and use it in GitHub Desktop.
Check if Hash include one or more of passed keys
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
class Hash | |
def has_one_of_keys1?(array=[]) | |
array.map {|v| self.has_key? v }.include? true | |
end | |
def has_one_of_keys2?(array=[]) | |
array.select { |v| self.has_key?(v) }.any? | |
end | |
def has_one_of_keys3?(array=[]) | |
array.any? { |v| self.has_key?(v) } | |
end | |
end | |
hash = { a: 1, b: 2 } | |
p hash.has_one_of_keys1?([:a, :c]) #=> true | |
p hash.has_one_of_keys2?([:a, :c]) #=> true | |
p hash.has_one_of_keys3?([:a, :c]) #=> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment