From: Robert Klemme Date: 2008-03-31T18:26:03+09:00 Subject: Re: How does Array#- do comparisons? 2008/3/31, Adam Bender : > How does Array#- (as in, [1, 2, 3, 1, 1] - [1]) compare two objects to > see if they are equal? I would think the .eql? method, but I've > written a class that implements that method so that two different > objects with the same values are considered equal and '-' isn't > functioning as I would expect. A contrived example: > > irb(main):004:0> class Hash > irb(main):005:1> def eql?(h) > irb(main):006:2> true > irb(main):007:2> end > irb(main):008:1> end > => nil > irb(main):010:0> h1 = {'a' => 1} > => {"a"=>1} > irb(main):011:0> h2 = {'a' => 1} > => {"a"=>1} > irb(main):012:0> h1 == h2 > => true > irb(main):013:0> h1.eql? h2 > => true > irb(main):014:0> [h1] - [h2] > => [{"a"=>1}] > > I would expect [] as a result, just like the following: > irb(main):015:0> ["asdf"] - ["asdf"] > => [] Another illustration of Stefano's point: irb(main):001:0> class Foo; end => nil irb(main):002:0> [Foo.new] - [Foo.new] => [#] irb(main):003:0> class Foo irb(main):004:1> def eql?(o) true end irb(main):005:1> end => nil irb(main):006:0> [Foo.new] - [Foo.new] => [#] irb(main):007:0> class Foo irb(main):008:1> def hash;0 end irb(main):009:1> end => nil irb(main):010:0> [Foo.new] - [Foo.new] => [] Kind regards robert -- use.inject do |as, often| as.you_can - without end