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]
on 2007-09-27 01:23
on 2007-09-27 05:35
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.
on 2007-09-27 17:33
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)?
on 2007-09-27 18:27
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.
on 2007-09-27 19:11
On 9/27/07, Yukihiro Matsumoto <matz@ruby-lang.org> wrote: > Embarrassing. > > matz. > > nobody is perfect :)
on 2007-10-02 22:47
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/
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.