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

ruby array difference
ruby array difference between elements
ruby compare two arrays for matches
ruby find difference between two strings
ruby match array
ruby compare array elements
rails check if two arrays are equal
ruby index of array

Let’s say I have this array with shipments ids.

s = Shipment.find(:all, :select => "id")

[#<Shipment id: 1>, #<Shipment id: 2>, #<Shipment id: 3>, #<Shipment id: 4>, #<Shipment id: 5>]

Array of invoices with shipment id's

i = Invoice.find(:all, :select => "id, shipment_id")

[#<Invoice id: 98, shipment_id: 2>, #<Invoice id: 99, shipment_id: 3>]
  • Invoices belongs to Shipment.
  • Shipment has one Invoice.
  • So the invoices table has a column of shipment_id.

To create an invoice, I click on New Invoice, then there is a select menu with Shipments, so I can choose "which shipment am i creating the invoice for". So I only want to display a list of shipments that an invoice hasn't been created for.

So I need an array of Shipments that don't have an Invoice yet. In the example above, the answer would be 1, 4, 5.

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

Find the only difference between two arrays in Ruby, I think this is the best built-in function to get what you expect (a - b).pop. Kyle Decot. Senior Software Engineer at Root Inc ruby-on-rails-3. Score 32. Posts 82. swift. How to get the difference between two arrays? Jan 15 '15. 113

a = [2, 4, 6, 8]
b = [1, 2, 3, 4]

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

Ruby, Array#-() is a Array class method which performs set difference operation by removing the similar elements of the two array. Syntax: Array.-(). Parameter: Arrays  Consider the following two arrays: A: {l, 2, 11, 15} B: {4, 12, 19, 23, 127, 235} 1. Suppose a pointer a points to the beginning of A and a pointer b points to the beginning of B. The current difference between a and bis 3. Store this as the min.

Use substitute sign

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

Creating a array from the difference of two arrays - Rails, I want to make an array out of the difference of two different arrays I (google ruby array difference) kim September 21, 2018, 8:04pm #3. Difference between Programmable Logic Array and Programming Array Logic; Maximum difference between two elements in an Array; Difference between highest and least frequencies in an array; Sum of minimum difference between consecutive elements of an array; Sort an array according to absolute difference with given value using Functors; Difference between String and Character array in Java; Difference between length of Array and size of ArrayList in Java; What is the difference between single

Ruby 2.6 is introducing Array.difference:

[1, 1, 2, 2, 3, 3, 4, 5 ].difference([1, 2, 4]) #=> [ 3, 3, 5 ]

So in the case given here:


Seems a nice elegant solution to the problem. I've been a keen follower of a - b | b - a, though it can be tricky to recall at times.

This certainly takes care of that.

Feature #11815: Proposal for method `Array#difference`, I propose that a method Array#difference be added to the Ruby core. Determine if two arrays of the same size contain the same elements a = [1,3,2,4​,3,4] u = a.uniq #=> [1, 2, 3, 4] u - a.difference(u) #=> [1, 2] I spend a fair bit of time answering Ruby questions on SO and would have reached for this method on many  Array.wrap(nil) # => [] Array.wrap( [1, 2, 3]) # => [1, 2, 3] Array.wrap(0) # => [0] This method is similar in purpose to Kernel#Array, but there are some differences: If the argument responds to to_ary the method is invoked. Kernel#Array moves on to try to_a if the returned value is nil, but Array.wrap returns an array with the argument as its single element right away.

The previous answer here from pgquardiario only included a one directional difference. If you want the difference from both arrays (as in they both have a unique item) then try something like the following.

def diff(x,y)
  o = x
  x = x.reject{|a| if y.include?(a); a end }
  y = y.reject{|a| if o.include?(a); a end }
  x | y

Advanced Ruby Arrays, In the Understanding Ruby Arrays we looked at the basics of arrays in Ruby. One area where this is particularly true involves the ability to create new arrays based on the union, intersection and difference of two arrays. This time, we only get elements that are common to both arrays. Last edited 3 years ago by Neil  When dealing with arrays of Strings, it can be useful to keep the differences grouped together. In which case, we can use Array#zip to group the elements together and then use a block to decide what to do with the grouped elements (Array).

Comparison of arrays in Ruby, There are different ways to work with arrays in Ruby. If you want to I have compiled for you different ways to compare arrays with others arrays or values. This operator get the intersection of two arrays, so if there are a coincidence we will get a non empty array. A = [1, 2] B = [1, 2, 3] (A - B).empty? Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array---that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

difference (Array), difference. Importance_1. Ruby latest stable (v2_5_5) - 0 notes - Class: Array collect; Importance_1_sm Returns a new array that is a copy of the receiver, removing any items that also appear in any of the 1, 1, 2, 2, 3, 3, 4, 5 ].​difference([ 1, 2, 4 ]) #=> [ 3, 3, 5 ] [ 1, 'c', :s, 'yep' ].difference([ 1 ], [ 'a', 'c' ]) #=> [ :s, "yep" ]. Difference array D[i] of a given array A[i] is defined as D[i] = A[i]-A[i-1] (for 0 < i < N ) and D[0] = A[0] considering 0 based indexing. Difference array can be used to perform range update queries "l r x" where l is left index, r is right index and x is value to be added and after all queries you can return original array from it.

Algorithms 101: Find the Difference between Two Arrays in JavaScript, Noob. v. Algorithms #23, a custom, speedy method for finding the difference in two arrays. difference between two arrays in red, image from Wikipedia. Today's So if we are given [4,3,2,7,8,2,3,1] , we want to compare it [1,2,3,4,5,6,7​,8]. Full-stack developer, Ruby, Rails, JavaScript, i love! React. 3 rail is is usually more robust and may have some out of scale details in addition to the oversize wheel flanges and sitting higher above the rails. It also will operate on sharper curves. Some 3 railers are now doing a compromise between 3 rail and 2 rail using wider curves, body mounted couplers and 2 rail trucks.

  • 1, 4, 5 is not a list of ids of invoices with no_shipment_id.
  • Sorry, corrected question. Thanks for considering it.
  • possible duplicate of Finding all records without associated ones
  • As of Ruby 2.6 you can use difference. More info here.
  • @SRack is the link you provided correct?
  • If you are using Rails 3.2.1+ and ActiveRecord you should use pluck: ids = i.pluck(:id)
  • This is exponentially slower than just doing x-i. The larger the arrays the slower it gets. Here's a benchmark I wrote comparing the two methods runnable.com/U5Y8g_nsUQokbzNl/benchmark-ruby-array-diff-methods
  • @Ryan - yes, but that's not the same thing.
  • This is the most elegant answer. a - b returns any elements in a that are not in b. b - a returns any elements in b that are not in a and the | returns the unique set of elements from those 2 results.
  • Love this :) Such a nice way to compare array differences
  • Great approach, nonetheless, this wont work for duplicate values. For example, if a included to two 4's the second 4 would not show up