From: Bob Hutchison Date: 2002-01-17T23:22:03+09:00 Subject: Strange change of behaviour Hi, I'm using Ruby 1.7.2 (Dec 27 or so). I've attached a bit of code below. There are four test result shown, the second and fourth work as I expected. The first and third do not. The first test unions two (different) empty lists into a list. The result is a list containing a single list. Whatever union is using to test appears to not distinguish between two different empty lists. So I have to assume that something other than object identity is used for testing. This is OK, I don't necessarily like it but I can live with it, that is, until you look at the third test. The third test unions two different empty hashes into a list. The result is a list containing two hashes. This is *not* what I would have expected given the results of the first test. But it gets more interesting. If you iterate over the list, the test finds that the list contains two elements that are both equal to the second hash added, but with different ids (this is as I would have expected). If you now do a difference (list - q) you wind up with an empty list. Now this is *really* surprising. So... 1) how does the | method decide if something is already in the list? 2) why are the | and - methods inconsistent? Is it because | and - decide differently if two things are the same? I think this must be it. Is this a good idea? I don't know if this is a bug or just me being silly. Cheers, Bob #### The test program def test title, p, q puts title list = [] list = list | [p] list = list | [q] printf " after unions: %s\n", list.inspect list.each do | x | printf " %s == q, %s (%s)\n", x.inspect, (x == q), x.id end list = list - [q] printf " after difference: %s\n\n", list.inspect end class Foo end test "Array Test 1", [], [] test "Array Test 2", [1], [2] test "Hash Test", {}, {} test "Class Test", Foo.new, Foo.new #### Output of running the above code: Array Test 1 after unions: [[]] [] == q, true (876960) after difference: [] Array Test 2 after unions: [[1], [2]] [1] == q, false (875124) [2] == q, true (875112) after difference: [[1]] Hash Test after unions: [{}, {}] {} == q, true (874596) {} == q, true (874584) after difference: [] Class Test after unions: [#, #] # == q, false (874140) # == q, true (874128) after difference: [#]