Ruby比较2个哈希值

时间:2014-10-09 18:32:19

标签: ruby-on-rails ruby hash

如果按键顺序不同,有没有办法比较2个哈希值?例如

hash1 = { "it"=> 10,"was"=>11,"the"=>14,"best"=>1,"of"=>12,"times"=>2,
          "worst"=>1,"age"=>2,"wisdom"=>1,"foolishness"=>1,"epoch"=>2,
          "belief"=>1,"incredulity"=>1,"season"=>2,"light"=>1,"darkness"=>1,
          "spring"=>1,"hope"=>1,"winter"=>1,"despair"=>1,"we"=>4,"had"=>2,
          "everything"=>1,"before"=>2,"us"=>2,"nothing"=>1,"were"=>2,"all"=>2,
          "going"=>2,"direct"=>2,"to"=>1,"heaven"=>1,"other"=>1,
          "way"=>1,"in"=>2,"short"=>1,"period"=>2,"so"=>1,"far"=>1,"like"=>1,
          "present"=>1,"that"=>1,"some"=>1,"its"=>2,"noisiest"=>1,
          "authorities"=>1,"insisted"=>1,"on"=>1,"being"=>1,
          "received"=>1,"for"=>2,"good"=>1,"or"=>1,"evil"=>1,"superlative"=>1,
          "degree"=>1,"comparison"=>1,"only"=>1 }


hash2 = {"superlative"=>1, "it"=>10, "going"=>2, "spring"=>1, "age"=>2, 
         "despair"=>1, "received"=>1, "good"=>1, "some"=>1, "worst"=>1, "was"=>11,
         "only"=>1,"us"=>2, "evil"=>1, "belief"=>1, "for"=>2, "darkness"=>1,
         "comparison"=>1, "short"=>1, "in"=>2, "present"=>1, "direct"=>2, "were"=>2,
         "way"=>1, "degree"=>1, "or"=>1, "of"=>12, "epoch"=>2, "incredulity"=>1,
         "period"=>2, "heaven"=>1, "other"=>1, "being"=>1, "its"=>2, "so"=>1,
         "authorities"=>1, "times"=>2, "we"=>4, "noisiest"=>1, "light"=>1, "hope"=>1,
         "foolishness"=>1, "everything"=>1, "far"=>1, "wisdom"=>1, "season"=>2, "like"=>1,
         "before"=>2, "had"=>2, "the"=>14, "nothing"=>1, "winter"=>1, "best"=>1,
         "that"=>1, "all"=>2, "insisted"=>1, "to"=>1, "on"=>1}

每个哈希都有相同的密钥。我将如何比较它们并确保每个键值都正确。我所看到的所有问题和答案都显示了具有相同顺序的键的哈希值。有关系吗?

我试图使用:

hash1_1 = hash1.select{|k,_| hash2.has_key? k}

吐出来的

{ "it"=>10, "was"=>11, "the"=>14, "best"=>1, "of"=>12, "times"=>2, 
     "worst"=>1, "age"=>2, "wisdom"=>1, "foolishness"=>1, "epoch"=>2, 
     "belief"=>1, "incredulity"=>1, "season"=>2, "light"=>1, 
     "darkness"=>1, "spring"=>1, "hope"=>1, "winter"=>1, "despair"=>1, 
     "we"=>4, "had"=>2, "everything"=>1, "before"=>2, "us"=>2, "nothing"=>1, 
     "were"=>2, "all"=>2, "going"=>2, "direct"=>2, "to"=>1, "heaven"=>1, 
     "other"=>1, "way"=>1, "in"=>2, "short"=>1, "period"=>2, "so"=>1, "far"=>1, 
     "like"=>1, "present"=>1, "that"=>1, "some"=>1, "its"=>2, "noisiest"=>1, 
     "authorities"=>1, "insisted"=>1, "on"=>1, "being"=>1, "received"=>1, 
     "for"=>2, "good"=>1, "or"=>1, "evil"=>1, "superlative"=>1, "degree"=>1, 
     "comparison"=>1, "only"=>1}

请帮我解释一下我需要做什么。谢谢。

3 个答案:

答案 0 :(得分:4)

无论键顺序如何,具有相同键/值对的两个哈希值都相等:

a = {:x => 1, :y => 2}
b = {:y => 2, :x => 1}

a == b
# => true

答案 1 :(得分:2)

您误解了结果。你的代码,工作正常。你的算法错了。

我将使用您的hash1和hash2并使用reject创建差异以消除它们之间匹配的所有内容:

hash1["it"] = 9
hash1["Tony"] = "great"
hash2["Jeff"] = "awesome"
hash1.delete "was"

diff_in_hash1 = hash1.reject{|k,v| hash2[k] == v}
# => {"it"=>9, "Tony"=>"great"}

diff_in_hash2 = hash2.reject{|k,v| hash1[k] == v}
# => {"it"=>10, "was"=>11, "Jeff"=>"awesome"}

你现在用它们做什么,取决于你。如果你得到空的哈希值,那么一切都是一样的。

还有一个gem called 'hashdiff'可以使用。

答案 2 :(得分:0)

据我所知,如果两个哈希之间有任何差异,你的主要目标是回答。因此,如果完全匹配(两个键和两个哈希值的对应值),则比较结果应为true,否则为false。如上所述here,因为Ruby 2.3。你可以使用:

{ a: 1 } <= { a: 1 } = true
{ a: 1 } <= { a: 2 } = false
{ a: 2 } <= { a: 1 } = false
{ a: 2 } <= { a: 2 } = true
{ a: 1 } >= { a: 1 } = true
{ a: 1 } >= { a: 2 } = false
{ a: 2 } >= { a: 1 } = false
{ a: 2 } >= { a: 2 } = true
{ a: 1 } <  { a: 1 } = false
{ a: 1 } <  { a: 2 } = false
{ a: 2 } <  { a: 1 } = false
{ a: 2 } <  { a: 2 } = false
{ a: 1 } >  { a: 1 } = false
{ a: 1 } >  { a: 2 } = false
{ a: 2 } >  { a: 1 } = false
{ a: 2 } >  { a: 2 } = false
{ a: 1 } == { a: 1 } = true
{ a: 1 } == { a: 2 } = false

{ a: 1, b: 2 } > { a: 1 } = true
{ a: 1, b: 2 } == { a: 1 } = false
{ a: 1, b: 2 } >= { a: 1 } = true
{ a: 1, b: 2 } > { a: 1, b: 5 } = false