说我有阵列a = ["a","b"]
和hashmap {"hello" => "world", "a" => "d"}
那将返回false,因为“hello”不在数组'a'中。
hashmap:{"a" => "hello", "a" => "world"}
没问题。
有没有办法在不手动完成所有工作的情况下执行此操作?例如:查找hashmap键是否是数组的子集?
答案 0 :(得分:3)
这将有效:
(hash.keys - a).empty?
# if returns true means all keys present in array.
# if returns false means all keys are not present in array.
答案 1 :(得分:0)
尝试这样的事情:
keys = hashmap.keys
(keys - a).empty?
结果'keys - a'是空的 - 这意味着所有键都在数组中
答案 2 :(得分:0)
a = ["a","b"]
b = {"hello" => "world", "a" => "d"}
(a-b.keys).empty?