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

后端 未结 6 603
天命终不由人
天命终不由人 2020-12-24 10:38

I\'m looking for a better way to do

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

prefer

6条回答
  •  别那么骄傲
    2020-12-24 11:06

    I like this way to solve this:

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

    It is fast and clear.

提交回复
热议问题