Bug #7045

DelegateClass array subtraction

Added by Ben Moss over 1 year ago. Updated over 1 year ago.

[ruby-core:47633]
Status:Closed
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:ruby 2.0.0dev (2012-09-21) [x86_64-darwin11.4.0] Backport:

Description

When I have an array that contains an array that contains a single instance of a class that inherits from DelegateClass, and I subtract from that array another array containing another array containing the same nested object, Ruby does not return an empty array. I've attached a test file that should replicate the one failing scenario, as well as some complimentary scenarios that do work as I had expected.

delegate_class_test.rb Magnifier (555 Bytes) Ben Moss, 09/22/2012 02:56 AM


Related issues

Duplicates ruby-trunk - Bug #6408: DelegateClass#eql? and <=> don't work as expected Assigned 05/07/2012

History

#1 Updated by Ben Moss over 1 year ago

The failing test is "testnestedarray_subtraction"

#2 Updated by Marc-Andre Lafortune over 1 year ago

The issue is that most classes' method eql? return false unless the object has the same class

require 'delegate'
o = SimpleDelegator.new(42)
o.eql?(o) # => false
# because the eql? is delegated, and
42.eql?(o) # => false

#3 Updated by Ben Moss over 1 year ago

I'm not sure that explains it. The first and third tests pass, and all of them used to pass in 1.8.

#4 Updated by Ben Moss over 1 year ago

This seems like a duplicate of http://bugs.ruby-lang.org/issues/6408. Not sure how to close this, but it can be closed.

#5 Updated by Marc-Andre Lafortune over 1 year ago

  • Status changed from Open to Closed

I'm not sure that explains it. The first and third tests pass

Sorry, I didn't focus on the other tests. So the reason the first and third test pass is that Hash lookup uses hash and eql?, except that if the objects are the same (same object_id), it assumes they are eql?. I'm assuming this is for performance reasons, since it should be true for almost all objects. It isn't for NaN, though, so this can give strange results:

n = 0.0/0
n.eql?(n) # => false
h = {n => :nan}
h[n] # => :nan (exact same object)
h[0.0/0] # => nil (since another float was constructed)

The first test passes because a hash is built with obj as the key, and afterwards lookup succeeds for obj as it is the exact same object.
The last test passes for the same reason, because the key and the lookup are done on the exact same array [obj].
The second test fails because the key and the lookup are not the same object (different object_id), so eql? is called and [obj].eql? [obj] returns false.

and all of them used to pass in 1.8.

In 1.8, eql? was not delegated at all. So you had obj.eql?(obj) # => true, but on the other hand SimpleDelegator.new(42).eql?(42) # => false.

This seems like a duplicate of http://bugs.ruby-lang.org/issues/6408. Not sure how to close this, but it can be closed.

Indeed, if 6408 was fixed, we'd have obj.eql? obj return true and all your tests would pass. I'll close this then.

Also available in: Atom PDF