Returning an array from Java to JRuby?

Howdy!

I’m running a JRuby script on top of a pretty hefty Java application,
and there are some cases where I’ll need to call a Java method from the
JRuby script, and have it return a bunch of objects. For example, I
might have a utility class that looks like this:

class UtilityClass {
public static String [] getStrings() {…}
}

… but if I call this from the Ruby side of things, the return value is
not recognized as an Array, and I get errors like:

NoMethodError: undefined method `join’ for
#<#Class:0x151f6292b:0x449a738a>

I’m still new to this, but that looks to me like the JRuby interpreter
has no information on the type of the return value, other than it being
a class. Any advice? I imagine I’m doing this in a strange way, and
I’m definitely not married to the idea of returning an array, as opposed
to some other Collection. I just figured that had the best chance of
working out of the box.

Thanks!
–Andy

Hi,

how do you call the Java method from JRuby ?

Regards
Roger

Am 18.08.2011 um 05:56 schrieb Andy I.:

Thanks Charlie!

That worked fine.

–Andy

On Wed, Aug 17, 2011 at 10:56 PM, Andy I. [email protected] wrote:

class UtilityClass {
public static String [] getStrings() {…}
}

… but if I call this from the Ruby side of things, the return value is
not recognized as an Array, and I get errors like:

NoMethodError: undefined method `join’ for
#<#Class:0x151f6292b:0x449a738a>

I have improved this output, but basically what you get back is just a
wrapped Java array, not a Ruby array. We don’t actively shove its
contents into a Ruby array for you.

Call to_a and you will get the contents as a Ruby Array.

  • Charlie