Ruby 比较基于哈希的元素和键与值,并返回不常见的元素

Ruby 比较基于哈希的元素和键与值,并返回不常见的元素,ruby,Ruby,我有一个给定目录中的文件数据库及其散列,我以json格式存储,如下所示 { "file_hashes": { "./file1": [ hash1, hash2, hash3 ], "./file2": [ hash1, hash2, hash3 ] } } 等等 我需要

我有一个给定目录中的文件数据库及其散列,我以json格式存储,如下所示

{
    "file_hashes": {
        "./file1": [
            hash1,
            hash2,
            hash3
        ],
        "./file2": [
            hash1,
            hash2,
            hash3
        ]
    }
 }
等等

我需要获取指定目录中文件的校验和,并将它们与数据库进行比较,然后返回两个哈希中都不存在的元素(即文件)


如何有效地比较两个散列并筛选不常见元素?

您是否在问如何计算对称差

给定两个可能有公共对和非公共对的哈希:

hash1 = {:a => :b, :c => :d}
hash2 = {:a => :b, :e => :f}
交叉口:

Hash[hash1.to_a & hash2.to_a]
=> {:a=>:b}
Hash[(hash1.to_a | hash2.to_a) - (hash1.to_a & hash2.to_a)]
=> {:c=>:d, :e=>:f}
工会:

Hash[hash1.to_a | hash2.to_a]
=> {:a=>:b, :c=>:d, :e=>:f}
对称差,使用并集-交集计算:

Hash[hash1.to_a & hash2.to_a]
=> {:a=>:b}
Hash[(hash1.to_a | hash2.to_a) - (hash1.to_a & hash2.to_a)]
=> {:c=>:d, :e=>:f}
对称差,使用差的并集计算:

Hash[(hash1.to_a - hash2.to_a) | (hash2.to_a - hash1.to_a)]
=> {:c=>:d, :e=>:f}

如果您的哈希值很大,或者是嵌套的,或者有其他复杂性,那么您需要阅读更好的解决方案。试试Ruby gem。

问题描述非常模糊。给出一个输入和输出示例,以准确显示您想要完成的任务。展示到目前为止您尝试编写的代码,并描述失败的地方。