7

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?

5
  • What is the signature of the Java method? If it returns a simple array you should be able to use it as-is. That looks like an array of arrays. Commented Feb 14, 2013 at 15:16
  • yes its a 3d array that gets returned Commented Feb 14, 2013 at 15:17
  • I'm confused what the issue is then; what are you trying to do with it, how are you actually using it, etc? AFAIK it should be an array of arrays (of arrays, if it's really a 3D array--the method signature I asked for really would be nice to know). Commented Feb 14, 2013 at 15:26
  • I want to out put into Rails to pass over via JSON. ANy sort of puts array comes back with [[[D rather than the data. Will a map work? Commented Feb 14, 2013 at 15:49
  • If you just puts an array you'll get the type signature--it's not going to magically format anything for you. If you want it as JSON, convert it to JSON. Commented Feb 14, 2013 at 15:55

1 Answer 1

9

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.