How do I compare two hashes?

前端 未结 14 1573
粉色の甜心
粉色の甜心 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:34

    If you want to get what is the difference between two hashes, you can do this:

    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"}
    

提交回复
热议问题