I modified a previous poster's program that used hashes (good idea!). 
Now there's only one hash, which counts the number of identical keys 
instead of actually storing the values. I then decrement the hash values 
for each key in b. If a and b are equal, all values in the hash should 
be 0. I also immediately reject as non-equal two arrays of different 
sizes. Seems to work for his test cases (and some I added), and I guess 
is probably smaller and faster. Are there any holes in this solution?

def content_eq a, b
  return false if a.length != b.length
  ah = Hash.new {|h,k| h[k] = 0 }
  a.each {|x| ah[x] += 1 }
  b.each {|x| ah[x] -= 1 }
  # Not equal if any non-zero values
  not ah.values.detect {|v| v != 0 }
end

pairs = [
  [ [1,2,3,4,"abc"],      [2,3,1,"abc",4]            ],
  [ [1,2,3],              [1,1,2,3]                  ],
  [ [1,2,3,nil],          [nil, nil, nil, 1,2,1,3,2] ],
  [ [],                   []                         ],
  [ [],                   [nil]                      ],
  [ [/^$/],               [/^$/]                     ],
  [ [/^$/],               [/^/]                      ],
  [ [nil, nil, nil, nil], [nil, nil, nil, nil]       ],
]

pairs.each{|a,b| puts "content_eq(#{ a.inspect }, #{ b.inspect }) #=> #{ 
content_eq a, b }"}

----
content_eq([1, 2, 3, 4, "abc"], [2, 3, 1, "abc", 4]) #=> true
content_eq([1, 2, 3], [1, 1, 2, 3]) #=> false
content_eq([1, 2, 3, nil], [nil, nil, nil, 1, 2, 1, 3, 2]) #=> false
content_eq([], []) #=> true
content_eq([], [nil]) #=> false
content_eq([/^$/], [/^$/]) #=> true
content_eq([/^$/], [/^/]) #=> false
content_eq([nil, nil, nil, nil], [nil, nil, nil, nil]) #=> true

-- 
Posted via http://www.ruby-forum.com/.