Issue Type: Bug Bug
Affects Versions: JRuby 1.7.0
Assignee: Thomas E Enebo
Created: 26/Oct/12 2:06 AM
Description:

It seems that the behaviour of == operator changed between jruby 1.6.8 and 1.7.0. See the following:

$ ruby -v
jruby 1.6.8 (ruby-1.8.7-p357) (2012-09-18 1772b40) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_37) [linux-amd64-java]
$ irb
jruby-1.6.8 :003 > require 'java'                                                                                                                                                              
 => false                                                                                                                                                                                      
jruby-1.6.8 :004 > '' == java.util.Date.new()                                                                                                                                                  
 => false                                                                                                                                                                                      
jruby-1.6.8 :005 > java.util.Date.new() == ''                                                                                                                                                  
 => nil

$ ruby -v
jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot(TM) 64-Bit Server VM 1.6.0_37-b06 [linux-amd64]                                                                                     
$ irb                                                                                                                                                                  
jruby-1.7.0 :001 > require 'java'                                                                                                                                                              
 => false                                                                                                                                                                                      
jruby-1.7.0 :002 > '' == java.util.Date.new()                                                                                                                                                  
 => false                                                                                                                                                                                      
jruby-1.7.0 :003 > java.util.Date.new() == ''
Java::JavaLang::ClassCastException: java.lang.String cannot be cast to java.util.Date
        from java.util.Date.compareTo(Date.java:112)
        from sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        from sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        from sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        from java.lang.reflect.Method.invoke(Method.java:597)
        from org.jruby.javasupport.JavaMethod.invokeDirectWithExceptionHandling(JavaMethod.java:455)
        from org.jruby.javasupport.JavaMethod.invokeDirect(JavaMethod.java:316)
        from org.jruby.java.invokers.InstanceMethodInvoker.call(InstanceMethodInvoker.java:61)
        from org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:326)
        from org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:170)
        from org.jruby.ast.FCallOneArgNode.interpret(FCallOneArgNode.java:36)
        from org.jruby.ast.NewlineNode.interpret(NewlineNode.java:105)
        from org.jruby.ast.BlockNode.interpret(BlockNode.java:71)
        from org.jruby.evaluator.ASTInterpreter.INTERPRET_METHOD(ASTInterpreter.java:75)
        from org.jruby.internal.runtime.methods.InterpretedMethod.call(InterpretedMethod.java:182)
        from org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java:192)
... 135 levels...
        from org.jruby.evaluator.ASTInterpreter.INTERPRET_METHOD(ASTInterpreter.java:75)
        from org.jruby.internal.runtime.methods.InterpretedMethod.call(InterpretedMethod.java:182)
        from org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java:192)
        from org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:326)
        from org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:170)

As for me:

  1. A == B should always behave in exactly the same way as B == A (equality should be symmetric by definition)
  2. comparing objects should never throw exceptions unless some class has buggy implementation of comparison

Here, I don't understand why Ruby's == calls Java compareTo() instead of equals(). java.util.Date.compareTo() is allowed to throw a ClassCastException() in this case, because Date implements Comparable<Date>, so its compareTo() method requires a Date instance as an argument due to the method signature. Personally I don't think that calling Java's compareTo() is a good idea in any case, even for numerical types (Integer etc.), because they always expect object of the same type, while Ruby's == operator checks if both objects are equal, allowing them to be of different types.

Environment: jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot(TM) 64-Bit Server VM 1.6.0_37-b06 [linux-amd64] run from rvm 1.16.17 (stable)
Project: JRuby
Priority: Major Major
Reporter: Piotr Kubowicz
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

Reply via email to