JRuby: how to call a Java variational function without optional arguments

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 (as this is optional), but my code is calling ArgumentError

( wrong # of arguments(1 for 2)

)

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

+2


a source to share


1 answer


Wrap varargs in 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 you only want to skip varargs pass an empty Java array instead

  jruby-1.4.0 > java.lang.System.out.format('foo ', [].to_java) 

      

+1


a source







All Articles