Skip to content

Instantly share code, notes, and snippets.

@zlw
Created August 25, 2011 12:17
Show Gist options
  • Save zlw/1170527 to your computer and use it in GitHub Desktop.
Save zlw/1170527 to your computer and use it in GitHub Desktop.
Check if Hash include one or more of passed keys
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