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

后端 未结 6 621
天命终不由人
天命终不由人 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 10:57

    Just in the spirit of TIMTOWTDI, here's another way. If you require 'set' (in the std lib) then you can do this:

    Set[:a,:b,:c,:d].subset? hash.keys.to_set
    

提交回复
热议问题