有办法做到这一点吗?这称为复合键吗?
{
"MyModel" : {
"type" : "object",
"properties" : {
"dummyAttrib" : {
"type" : "object",
"description" : "dummyAttrib"
}
}
}
}
答案 0 :(得分:1)
Hash
内置了通配符解释。您可以使用以下内容实现自己的目标:
class MyHash < Hash
def select_composite(key)
mkeys = matching_keys(key)
select { |k, _| mkeys.include?(k) }
end
private
def matching_keys(key)
keys.select { |hkey| matching_key?(hkey, key) }
end
def matching_key?(hkey, key)
elements = Array(key)
Array(hkey).each_with_index.select { |helement, i|
helement == elements[i] || elements[i] == '*'
}.count == elements.count
end
end
a = MyHash[{ %w(key1 key2) => "stuff" }]
a.select_composite(%w(key1 *))
答案 1 :(得分:0)
不,没有。您需要对哈希中的所有键进行线性搜索,以查找符合您特定条件的键。
嵌套哈希后你真正的目标是什么:
a = {
key1: {
key2: "stuff"
}
}
a[:key1][:key2] # "stuff"
a[:key1] # { key2: "stuff" }