Greg Guida Greg Guida - 1 year ago 88
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 Source
[:a, :b, :c, :d].all? {|s| hash.key? s}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download