HashKeys@github - taki sobie o bajer z nudów. Użycie jest bajecznie proste - pobieramy hash_keys.rb (może zrobię z tego gema w wolnym czasie) a następnie:
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
require 'hash_keys' | |
class Hash | |
include HashKeys | |
end | |
hash = { a: 1, b: 2 } | |
p hash.has_keys? [:a, :c] #=> { a: true, c: false } | |
p hash.has_keys [:c, :d] #=> false | |
p hash.any_of_keys? [:a, :c] #=> true |
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 |