I did most of the initial work without any SCM. I am so bad.
[monoslider.git] / lib / array_case_equality.rb
blob06f7714bdc1c0128dd7b6d2cddd1c2f47ec366b0
1 class Array
2   
3   alias :case_equal? :===
4   
5   def ===(other, counter = [0], my_rec = {}, his_rec = {}) # !> method redefined; discarding old eql?
6     return true if other.object_id == self.object_id
7     return false unless other.is_a? Array
8     return true if other == self
9     return false unless  other.size == self.size
10     
11     counter[0] += 1
12     my_rec[object_id] = his_rec[other.object_id] = counter[0]
13     
14     size.times do |i|
15       x, y = self[i], other[i]
16       xid, yid = x.object_id, y.object_id
17       if x.is_a? Array and y.is_a? Array
18         recursive = my_rec[xid] || his_rec[yid]
19         if recursive && my_rec[xid] != his_rec[yid]
20           return false
21         elsif recursive # matches because the two arrays have same object_id
22           next
23         else # not recursive, just compare normally
24           return false unless x.===(y, counter, my_rec, his_rec)
25         end
26       elsif x.is_a? Array
27         # if x is an Array, but y is not
28         return false
29       else
30         # general case
31         return false unless x === y
32       end
33     end
34     
35     true
36   end
37 end