From: Colin Bartlett Date: 2009-10-15T05:03:25+09:00 Subject: Re: how to solve a special JRuby and Java syntax conflict? I'm not sure whether the following will help or not. I'm not a JRuby or Java expert, but I am using both. I'm developing something which does numeric calculations in Java for speed, but which uses JRuby for easy scripting and interractive (jirb) work, and I need to set up numeric arrays which "stay" in Java, but I haven't got around to testing populating the Java arrays from JRuby. Your original question prompted me to try it, and the following seems to work. (Ignore the non-array bits!) Is this the sort of thing you are trying to do? Colin Bartlett // Java code, which I compiled to a class, and converted to a jar package path_to_dir_of_java_file; import java.io.*; public class ActCal_value { public double float_value; public double[][] arr; // constructor public ActCal_value(double float_value_v) { float_value = float_value_v; } public void set_arr_dims(int dim1, int dim2) { arr = new double[dim1][dim2]; } public void set_arr_val(int index1, int index2, double vv) { arr[index1][index2] = vv; } public void print_arr() throws IOException { System.out.println("* arr=="); for( int i0 = 0; i0 < arr.length; i0++) { System.out.print("* arr[" + i0 + "]"); for( int i1 = 0; i1 < arr[i0].length; i1++) { System.out.print( " [" + i1 + "]=" + arr[i0][i1] + ";"); } System.out.println( "" ); } } } # Ruby code, setting up Java array using above Java method set_arr_dims, # and populating Java array arr in two ways: # (1) using above Java method set_arr_val; # (2) using Ruby array element access; include Java require "path_to_jar/jar_file.jar" include_class "jar_path.ActCal_value" acv = ActCal_value.new( 42.3 ) puts "** acv = " + acv.inspect p acv.float_value aa = [ [0.0, 0.1, 0.2], [1.0, 1.1, 1.2] ] acv.set_arr_dims(aa.size, aa[0].size) acv.print_arr() aa.each_with_index do | ar, idx0 | ar.each_index do | idx1 | acv.set_arr_val( idx0, idx1, aa[idx0][idx1] ) end end acv.print_arr() puts acv.arr[1][2] acv.arr[1][2] = 11.22 acv.print_arr() puts acv.arr[1][2] aa << [2.0, 2.1, 2.2] acv.set_arr_dims(aa.size, aa[0].size) aa.each_with_index do | ar, idx0 | ar.each_index do | idx1 | acv.arr[idx0][idx1] = aa[idx0][idx1] end end acv.print_arr() puts acv.arr[1][2]