On 11/22/06, Daniel N <has.sox / gmail.com> wrote: > I want to check to see if two arrays contain the same values. > That is, are they the same array but not in the same order. > > Example > equal > [1,2,3,4,"abc"] should equal [2,3,1,"abc",4] and all the other different > orders that are possible > > This should not be equal > [1,2,3] and [1,1,2,3] > [1,2,3,nil] and [nil, nil, nil, 1,2,1,3,2] > > At first I thought of > (( array1 | array2 ) - array1).size == 0 > > but this does not take into account duplicate values and the should not > equals do equal :(. In fact I have not been able work out how to use any of > the standard array, or Enumerable methods in such a way I can check if two > arrays contain the same values as well as taking into account duplicates. > > Any pointers? What's wrong with: irb(main):002:0> require 'set' => true irb(main):003:0> t1 = [1,2,3,4,"abc"] => [1, 2, 3, 4, "abc"] irb(main):004:0> t2 = [2,3,1,"abc",4] => [2, 3, 1, "abc", 4] irb(main):005:0> s1 = Set.new(t1) => #<Set: {"abc", 1, 2, 3, 4}> irb(main):006:0> s2 = Set.new(t2) => #<Set: {"abc", 1, 2, 3, 4}> irb(main):007:0> s1 == s2 => true irb(main):008:0> s1.subset?(s2) => true irb(main):009:0> s2.subset?(s1) => true > > Cheers > Daniel > > -- Stuart Yarus <syarus at gmail dot com>