Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Array in Jruby

Tags:

java

ruby

jruby

I have a Java lib that I am pulling some data out of.

It out puts a 3D array. However I can not do anything with it.

[[D[]@5615a6e0

is the response I get. I have tried mapping it:

{ |arr| arr.map { |arr| arr.to_a } }

but i get nothing out, What is the best way to parse this java array for ruby use?

like image 255
Charlie Davies Avatar asked Feb 14 '13 15:02

Charlie Davies


1 Answers

Should not be a problem. Just use to_a

Java Code:

package com.test.sof;

public class MyTest {
    public static int[] ReturnTestArray() {
        int[] anArray = new int[3];
        anArray[0] = 1;
        anArray[1] = 2;
        anArray[2] = 3;
        return anArray;
    }
}

JRuby Code:

require 'java'
java_import com.test.sof.MyTest

java_array = MyTest.ReturnTestArray
p java_array
#=> int[1, 2, 3]@484c6b

ruby_array = Array.new
p ruby_array
#=> []
ruby_array = java_array.to_a

p ruby_array.size
#=> 3
p ruby_array.join(', ')
#=> "1, 2, 3"
like image 65
Sully Avatar answered Sep 30 '22 16:09

Sully