Rails 3. How to get the difference between two arrays? Rails 3. How to get the difference between two arrays? arrays arrays

Rails 3. How to get the difference between two arrays?


a = [2, 4, 6, 8]b = [1, 2, 3, 4]a - b | b - a # => [6, 8, 1, 3]


First you would get a list of shipping_id's that appear in invoices:

ids = i.map{|x| x.shipment_id}

Then 'reject' them from your original array:

s.reject{|x| ids.include? x.id}

Note: remember that reject returns a new array, use reject! if you want to change the original array


Use substitute sign

irb(main):001:0> [1, 2, 3, 2, 6, 7] - [2, 1]=> [3, 6, 7]