Forum: Ruby-core Array#-, &, |, uniq don't use ==

4995c7ce9523ce00dea01362cb976196?d=identicon&s=25 murphy (Guest)
on 2007-09-27 01:23
(Received via mailing list)
Hello!

Let's say we define an object x which is equal to everything:

  x = Object.new
  def x.== other
    true
  end

Using this element in array operations gives me unexpected results:

  # as expected
  x == 3  # => true
  [1, 2, 3] == [x, x, x]  # => true
  [1, 2, 3] === [x, x, x]  # => true
  [1, 2, 3].include? x  # => true
  [1, 2, 3].index x  # => 0
  [1, 2, 3].delete(x)  # => #<Object:0x48ea68>

  # unexpected
  [1, 2, 3] - [x]  # => [1, 2, 3]
  [1, 2, 3] & [x]  # => []
  [1, 2, 3] | [x]  # => [1, 2, 3, #<Object:0x48ea68>]
  [1, 2, 3, x].uniq  # => [1, 2, 3, #<Object:0x48ea68>]

Is this a feature? Why do some methods use "=="-equality and others
"eql?"-equality?

[murphy]
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2007-09-27 05:35
(Received via mailing list)
Hi,

In message "Re: Array#-, &, |, uniq don't use =="
    on Thu, 27 Sep 2007 08:22:48 +0900, murphy <murphy@rubychan.de>
writes:

|Is this a feature? Why do some methods use "=="-equality and others
|"eql?"-equality?

It's a feature, to reduce complexity order from O(m^n) to O(mn).

              matz.
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2007-09-27 17:33
(Received via mailing list)
On Thu, Sep 27, 2007 at 12:35:25PM +0900, Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: Array#-, &, |, uniq don't use =="
>     on Thu, 27 Sep 2007 08:22:48 +0900, murphy <murphy@rubychan.de> writes:
>
> |Is this a feature? Why do some methods use "=="-equality and others
> |"eql?"-equality?
>
> It's a feature, to reduce complexity order from O(m^n) to O(mn).

Shouldn't that be O(mn) and O(m+n)?
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2007-09-27 18:27
(Received via mailing list)
Hi,

In message "Re: Array#-, &, |, uniq don't use =="
    on Fri, 28 Sep 2007 00:32:44 +0900, Mauricio Fernandez <mfp@acm.org>
writes:

|> It's a feature, to reduce complexity order from O(m^n) to O(mn).
|
|Shouldn't that be O(mn) and O(m+n)?

Exactly.  I have accidentally disclosed my math inability.
Embarrassing.

              matz.
48d1aca7191f2d16e184971054c7c143?d=identicon&s=25 Meinrad Recheis (Guest)
on 2007-09-27 19:11
(Received via mailing list)
On 9/27/07, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Embarrassing.
>
>                                                         matz.
>
>
nobody is perfect :)
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-10-02 22:47
(Received via mailing list)
On 9/26/07, murphy <murphy@rubychan.de> wrote:
>
>   # as expected
>   x == 3  # => true
>   [1, 2, 3] == [x, x, x]  # => true
>   [1, 2, 3] === [x, x, x]  # => true
>   [1, 2, 3].include? x  # => true
>   [1, 2, 3].index x  # => 0
>   [1, 2, 3].delete(x)  # => #<Object:0x48ea68>

Actually, some of these surprise me, since although x might == every
object, every object does not == x

x = Object.new
def x.== other
  true
end

x == "abc" # => true
"abc" == x # => false
x == :symbol # => true
:symbol == x # => false
x == nil # => true
nil == x # => false
x == 3 # => true
3 == x # => true

The last, and the one which makes your examples work "as expected" is
an artifact of the implementation of FixNum#==  I'm not sure whether
it's a bug or not, it's definitely an edge  case.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
This topic is locked and can not be replied to.