我如何比较两个哈希?

我正在尝试使用以下代码比较两个Ruby哈希:

#!/usr/bin/env ruby require "yaml" require "active_support" file1 = YAML::load(File.open('./en_20110207.yml')) file2 = YAML::load(File.open('./locales/en.yml')) arr = [] file1.select { |k,v| file2.select { |k2, v2| arr << "#{v2}" if "#{v}" != "#{v2}" } } puts arr 

屏幕上的输出是来自file2的完整文件。 我知道一个事实,即文件是不同的,但脚本似乎并没有拿起它。

你可以直接比较哈希值是否相等:

 hash1 = {'a' => 1, 'b' => 2} hash2 = {'a' => 1, 'b' => 2} hash3 = {'a' => 1, 'b' => 2, 'c' => 3} hash1 == hash2 # => true hash1 == hash3 # => false hash1.to_a == hash2.to_a # => true hash1.to_a == hash3.to_a # => false 


你可以将哈希转换为数组,然后得到它们的区别:

 hash3.to_a - hash1.to_a # => [["c", 3]] if (hash3.size > hash1.size) difference = hash3.to_a - hash1.to_a else difference = hash1.to_a - hash3.to_a end Hash[*difference.flatten] # => {"c"=>3} 

进一步简化:

通过三元结构分配差异:

  difference = (hash3.size > hash1.size) \ ? hash3.to_a - hash1.to_a \ : hash1.to_a - hash3.to_a => [["c", 3]] Hash[*difference.flatten] => {"c"=>3} 

在一个操作中完成所有操作,摆脱differencevariables:

  Hash[*( (hash3.size > hash1.size) \ ? hash3.to_a - hash1.to_a \ : hash1.to_a - hash3.to_a ).flatten] => {"c"=>3} 

你可以试试hashdiff gem,它可以在哈希中对哈希和数组进行深度比较。

以下是一个例子:

 a = {a:{x:2, y:3, z:4}, b:{x:3, z:45}} b = {a:{y:3}, b:{y:3, z:30}} diff = HashDiff.diff(a, b) diff.should == [['-', 'a.x', 2], ['-', 'a.z', 4], ['-', 'b.x', 3], ['~', 'b.z', 45, 30], ['+', 'b.y', 3]] 

如果你想得到两个哈希值之间的区别,你可以这样做:

 h1 = {:a => 20, :b => 10, :c => 44} h2 = {:a => 2, :b => 10, :c => "44"} result = {} h1.each {|k, v| result[k] = h2[k] if h2[k] != v } p result #=> {:a => 2, :c => "44"} 

Rails 不赞成 diff方法。

快速的一行:

 hash1.to_s == hash2.to_s 

你可以使用一个简单的数组交集,这样你就可以知道每个散列有什么不同。

  hash1 = { a: 1 , b: 2 } hash2 = { a: 2 , b: 2 } overlapping_elements = hash1.to_a & hash2.to_a exclusive_elements_from_hash1 = hash1.to_a - overlapping_elements exclusive_elements_from_hash2 = hash2.to_a - overlapping_elements 

这是在“ 比较ruby哈希 ”中回答的。 Rails添加一个diff方法到哈希。 它运作良好。

我有同样的问题,并发送拉请求导轨

  • 与深度嵌套散列工作
  • 与散列数组一起工作

https://github.com/elfassy/rails/commit/5f3410e04fe8c4d4745397db866c9633b80ccec6

如果你需要一个快速和肮脏的差异之间的正确支持零值你可以使用类似的东西

 def diff(one, other) (one.keys + other.keys).uniq.inject({}) do |memo, key| unless one.key?(key) && other.key?(key) && one[key] == other[key] memo[key] = [one.key?(key) ? one[key] : :_no_key, other.key?(key) ? other[key] : :_no_key] end memo end end 

如果你想要一个很好格式化的差异,你可以这样做:

 # Gemfile gem 'awesome_print' # or gem install awesome_print 

在你的代码中:

 require 'ap' def my_diff(a, b) as = a.ai(plain: true).split("\n").map(&:strip) bs = b.ai(plain: true).split("\n").map(&:strip) ((as - bs) + (bs - as)).join("\n") end puts my_diff({foo: :bar, nested: {val1: 1, val2: 2}, end: :v}, {foo: :bar, n2: {nested: {val1: 1, val2: 3}}, end: :v}) 

这个想法是使用真棒打印格式,并区分输出。 差异不是确切的,但是对于debugging目的是有用的。

…现在以模块forms被应用到各种集合类(哈希其中)。 这不是一个深入的检查,但很简单。

 # Enable "diffing" and two-way transformations between collection objects module Diffable # Calculates the changes required to transform self to the given collection. # @param b [Enumerable] The other collection object # @return [Array] The Diff: A two-element change set representing items to exclude and items to include def diff( b ) a, b = to_a, b.to_a [a - b, b - a] end # Consume return value of Diffable#diff to produce a collection equal to the one used to produce the given diff. # @param to_drop [Enumerable] items to exclude from the target collection # @param to_add [Enumerable] items to include in the target collection # @return [Array] New transformed collection equal to the one used to create the given change set def apply_diff( to_drop, to_add ) to_a - to_drop + to_add end end if __FILE__ == $0 # Demo: Hashes with overlapping keys and somewhat random values. Hash.send :include, Diffable rng = Random.new a = (:a..:q).to_a.reduce(Hash[]){|h,k| h.merge! Hash[k, rng.rand(2)] } b = (:i..:z).to_a.reduce(Hash[]){|h,k| h.merge! Hash[k, rng.rand(2)] } raise unless a == Hash[ b.apply_diff(*b.diff(a)) ] # change b to a raise unless b == Hash[ a.apply_diff(*a.diff(b)) ] # change a to b raise unless a == Hash[ a.apply_diff(*a.diff(a)) ] # change a to a raise unless b == Hash[ b.apply_diff(*b.diff(b)) ] # change b to b end 

另一个更简单的方法如何:

 require 'fileutils' FileUtils.cmp(file1, file2)