Have a Java class with an overloaded method:
public void test(float x) {
}
public void test(RubyArray array) {
}
In Ruby,
object.test(1.0)
Works fine. However:
object.test([1.0,2.0])
Will crash with the message:
org.jruby.exceptions.RaiseException: (NameError) no method 'test' for arguments (org.jruby.RubyArray) on Java::MyNamespace::MyClass
available overloads:
(org.jruby.RubyArray)
(int)
But, as you can see, that doesn't make any sense. It is telling me that there is no method for my argument of type RubyArray
, and then it promptly tells me that there is an available overload for arguments of type RubyArray
.
Interestingly, this works fine if you remove the (int)
overload, that is, it works if the method that accepts RubyArray
has no other overloads.
I have created many overloaded Java methods and used them in Ruby before, so I am guessing that the problem is related mainly to the RubyArray
argument itself - although I don't see the issue.
Why is that? What am I doing wrong here?
I am using JRuby 1.7.11
, building for what I think is Java SE 6
on Eclipse Kepler for Mac.
To make it easier to test, you can run the following two programs:
This works:
import javax.script.*;
import org.jruby.RubyArray;
public class Main {
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
Main app = new Main();
}
public Main() throws ScriptException, NoSuchMethodException {
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
jruby.eval("include Java;\n" + "def start(main) main.test([1,2,3]); end");
Invocable invocable = (Invocable)jruby;
invocable.invokeFunction("start",this);
}
public void test(RubyArray array) {
System.out.println(array);
}
}
This doesn't work:
import javax.script.*;
import org.jruby.RubyArray;
public class Main {
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
Main app = new Main();
}
public Main() throws ScriptException, NoSuchMethodException {
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
jruby.eval("include Java;\n" + "def start(main) main.test([1,2,3]); end");
Invocable invocable = (Invocable)jruby;
invocable.invokeFunction("start",this);
}
public void test(int x) {
System.out.println(x);
}
public void test(RubyArray array) {
System.out.println(array);
}
}