Ruby中深度嵌套的哈希差异

时间:2014-06-13 05:25:33

标签: ruby associative-array

我想找到两种不同哈希之间的区别。

a = {"3"=>{"passenger_type"=>"ADT", "the_order"=>"3", "last"=>"ARUN", "first"=>"KUMAR", "middle"=>nil}, "2"=>{"passenger_type"=>"ADT", "the_order"=>"2", "last"=>"JONES", "first"=>"MAXIM", "middle"=>nil}, "1"=>{"passenger_type"=>"ADT", "the_order"=>"1", "last"=>"RAM", "first"=>"TODD", "middle"=>nil}}

b = {1=>{"middle"=>nil, "the_order"=>"1", "passenger_type"=>"BDT", "last"=>"RAM", "first"=>"TODD"}, 2=>{"middle"=>nil, "the_order"=>"2", "passenger_type"=>"ADT", "last"=>"JONES", "first"=>"MAXIM"}, 3=>{"middle"=>nil, "the_order"=>"3", "passenger_type"=>"ADT", "last"=>"ARUN", "first"=>"KUMAR"}}

并且生成的散列或数组应该类似于以下

{1=>{"middle"=>nil, "the_order"=>"1", "passenger_type"=>"BDT", "last"=>"RAM", "first"=>"TODD"}}

1 个答案:

答案 0 :(得分:3)

以下给出了答案,但不确定它是否适合您的实际问题。您可能需要优化帖子,让其他人知道您问题的详细信息。

b.select{|k, v| v != a[k.to_s]}