How does Ruby's Array.| compare elements for equality?

Posted by Max Howell on Stack Overflow See other posts from Stack Overflow or by Max Howell
Published on 2010-03-27T15:45:09Z Indexed on 2010/03/27 15:53 UTC
Read the original article Hit count: 94

Filed under:

Here's some example code:

class Obj
  attr :c, true

  def == that
    p '=='
    that.c == self.c
  end
  def <=> that
    p '<=>'
    that.c <=> self.c
  end
  def equal? that
    p 'equal?'
    that.c.equal? self.c
  end
  def eql? that
    p 'eql?'
    that.c.eql? self.c
  end
end

a = Obj.new
b = Obj.new

a.c = 1
b.c = 1

p [a] | [b]

It prints 2 objects but it should print 1 object. None of the comparison methods get called. How is Array.| comparing for equality?

© Stack Overflow or respective owner

Related posts about ruby