检查散列的键是否包含所有一组键
问题描述:
我正在寻找更好的方法
if hash.key? :a &&
hash.key? :b &&
hash.key? :c &&
hash.key? :d
最好是这样的
hash.includes_keys? [ :a, :b, :c, :d ]
我想出了
hash.keys & [:a, :b, :c, :d] == [:a, :b, :c, :d]
但我不喜欢必须将数组添加两次
but I dont like having to add the array twice though
答
%i[a b c d].all? {|s| hash.key? s}