Let's say I have two arrays filled with hashes,
array_a = [{'key' => 'a'}, {'key' => 'b'}, {'key' => 'c'}, {'key' => 'd'}]
array_b = [{'key' => 'a'}, {'key' => 'b'}, {'key' => 'd'}]
how do I compare array_a
and array_b
and return the hash that was not found in array_b
.
So the compare should return:
# => [{'key' => 'c'}]
Array#-
would work:
array_a = [{'key' => 'a'}, {'key' => 'b'}, {'key' => 'c'}, {'key' => 'd'}]
array_b = [{'key' => 'a'}, {'key' => 'b'}, {'key' => 'd'}]
array_a - array_b
#=> [{"key"=>"c"}]
The method "… compares elements using their hash
and eql?
methods …" and according to Hash#hash
:
Two hashes with the same content will have the same hash code (and will compare using
eql?
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.