views:

178

answers:

1

I have a function with a signature similar to String.format(String, Object...).

I want to call this function from JRuby without the last parameters (since it is optional), but my code throws an ArgumentError (wrong # of arguments(1 for 2))

Is there a way to call this function with only 1 argument just like I would do in Java?

A: 

Wrap the varargs into a Java array

  jruby-1.4.0 > java.lang.System.out.format('foo %d, %d, %d, %d, %d', [1, 2, 3, 4, 5].to_java)
  foo 1, 2, 3, 4, 5 => #<Java::JavaIo::PrintStream:0x79ef3ccd> 

If all you want is to skip the varargs, pass an empty Java array instead

  jruby-1.4.0 > java.lang.System.out.format('foo ', [].to_java) 
Julik
I already figured I could do that. What I want is being able to call the method without the array argument. Currently, I am re-opening every class by hand to change the method signature.
Vincent Robert
I don't think it is possible for Jruby to do that for you automatically - look at their bugtracker, there was a mention of this specifically
Julik