Greg Guida Greg Guida - 4 months ago 6
Ruby Question

Check if a hash's keys include all of a set of keys

I'm looking for a better way to do

if hash.key? :a &&
hash.key? :b &&
hash.key? :c &&
hash.key? :d


preferably something like

hash.includes_keys? [ :a, :b, :c, :d ]


I came up with

hash.keys & [:a, :b, :c, :d] == [:a, :b, :c, :d]


but I dont like having to add the array twice though

Answer
[:a, :b, :c, :d].all? {|s| hash.key? s}
Comments