【发布时间】:2015-05-19 15:23:38
【问题描述】:
假设我有数组a = ["a","b"]
还有哈希图{"hello" => "world", "a" => "d"}
这将返回 false,因为 "hello" 不在数组 'a' 中。
哈希图:{"a" => "hello", "a" => "world"} 很好。
有没有办法在不手动完成所有工作的情况下做到这一点? eg: 查找 hashmap 键是否是数组的子集?
【问题讨论】:
-
您应该将对象
{"hello" => "world", "a" => "d"}称为“哈希”(即Hash的实例)。 “hashmap”(又名“hash table”)指的是数据结构。 -
{"a" => "hello", "a" => "world"}不是“好”,因为哈希不能有重复的键。 :-) 你也可以写hash.values_at(*a).size == hash.size,但是)hash.keys-a).empty?更清楚。