How to compare content of two arrays?

> [1,2,3] <=> [1,2,3]
=> 0
> [1,2,3] <=> [2,2,3]
=> -1
> [1,2,3] <=> [3,2,3]
=> -1
> [1,2,3] <=> [1,3,3]
=> -1
> [1,2,3] <=> [1,1,3]
=> 1

this is from RailsThinker Post and has been working for me very well.


Below I'm using the all? operator on an array, which will return true if all of the items in the array return true for the block I'm passing in.

my_zip = [1,2,3,4,5,6]
[2,3,5].all?{|z| my_zip.include?(z)}
=> true 
[20,3,5].all?{|z| my_zip.include?(z)}
=> false

You'd just change it up to be the user's zip codes


You can do array differences, if the result is the empty array, the 2 arrays contained the same elements:

>> [1,2,3]-[3,1,2] #=> []

If you still have elements left, then not all elements of the first array were present in the second one:

>> [1,2,5]-[3,1,2] #=> [5]