Find difference between arrays in Ruby, where the elements are hashes -
i have 2 arrays of hashes find difference between. issue array elements single item hashes.
so far, using array1 - array2 appears working correctly need watch out gotchas here? hash elements read h = {'id' => '76322'}, numeric value differs hash hash, nothing fancy.
[edit]
here's i'm looking for:
array1 = [] array2 = [] h = {'id' => '76322'} array1.push(h) h = {'id' => '7891'} array1.push(h) array2.push(h) array1 = array1 - array2 # should result in array1 having single hash of values {'id', '76322'}
array1 - array2 works putting elements of array2 temporary hash, returning elements of array1 don't appear in hash. hash elements compared using == determine whether match.
comparing 2 hashes == gives true if keys , values of hashes match using ==. so
h1 = {'id' => '7891'} h2 = {'id' => '7891'} h1 == h2 evaluates true, though h1 , h2 different hashes, , corresponding elements correctly removed.
the consideration can think of if always have strings everywhere in hash keys , values. if integers, or symbols, {:id => 7891} aren't going results want, because :id == 'id' , '7891' == 7891 both false.
Comments
Post a Comment