How do I compare two hashes?

前端 未结 14 1526
粉色の甜心
粉色の甜心 2020-11-30 00:06

I am trying to compare two Ruby Hashes using the following code:

#!/usr/bin/env ruby

require \"yaml\"
require \"active_support\"

file1 = YAML::load(File.op         


        
14条回答
  •  暖寄归人
    2020-11-30 00:51

    If you need a quick and dirty diff between hashes which correctly supports nil in values you can use something like

    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
    

提交回复
热议问题