From: Robert Klemme Date: 2007-06-10T19:50:35+09:00 Subject: Re: Overloading Array Subtraction operator On 10.06.2007 10:25, Nicko wrote: > Wow! > Thank you both! > > I ended up with > > class SuperArray < Array > def -(other) > self.select{|x| !other.include? x} > end > end > > and it works great :) I can optimise it later :) It is usually not such a good idea to inherit base classes like Array and Hash. Here are two more healthy approaches. 1. wrap Array with a class that represents the concept (which one btw?) your Array is used for. Then implement #- (and all the other methods). 2. wrap Hash with a class that represents the concept (which one btw?) your Hash is used for. Then implement #==, #hash and #eql? accordingly. The basic reason why your code does not work as you would like it to work is that Hash does not implement #eql? and #hash in a way that considers Hash content (for the reasons please search the archives, the topic has come up frequently). Note: irb(main):037:0> h={:foo=>:bar} => {:foo=>:bar} irb(main):038:0> h == h.dup => true irb(main):039:0> h.eql? h.dup => false irb(main):040:0> h.hash == h.dup.hash => false Kind regards robert