--001636b2b8e7b642cb0475e94a14 Content-Type: text/plain; charset=ISO-8859-1 ER....wups...sorry...I thought I was replying on the JRuby list...that's why I said "most of the people on this list have worked with JRuby". Again, sorry for the error, my bad. -Mario. -- I want to change the world but they won't give me the source code. On Wed, Oct 14, 2009 at 20:20, Mario Camou <mcamou / tecnoguru.com> wrote: > On Wed, Oct 14, 2009 at 11:51, Axel Etzold <AEtzold / gmx.de> wrote: > >> I really need some advice from somebody who has worked with JRuby here, >> I guess. I just cannot find a way of writing a Java Matrix without curly >> braces, yet the Ruby part of JRuby interprets curly braces as Hashes. > > > I think most of the people on this list have worked with JRuby. > > Here's the thing. If you're programming in JRuby, you're still programming > in Ruby. So you need to use Ruby syntax, not Java syntax. Even though JRuby > works on top of the JVM, it's still an implementation of the Ruby language, > and you need to use Ruby notation. > > That means: curly braces are for Hashes, square brackets are for Arrays. > > array 1, 2, 3, 4] > > Now for a two-dimensional array (or matrix) you still need to use the Ruby > syntax: > > matrix [ 1.1, 1.2, 1.3, 1.4 ], [ 2.1, 2.2, 2.3, 2.4 ], [ 3.1, 3.2, 3.3, > 3.4 ] ] > > Now, this will give you a Ruby Array, not a Java Array so you have to do > some manipulation to convert this into a Java Array. Since Ruby is > dynamically-typed, you need to tell JRuby what Java type to convert this > Array to, so you need to do is use the to_java(class) method: > > matrix [ 1.1, 1.2, 1.3, 1.4 ], [ 2.1, 2.2, 2.3, 2.4 ], [ 3.1, 3.2, 3.3, > 3.4 ] ].to_java > > The problem with this is that you'lll get an Array of Object and not an > Array of Arrays of doubles. You need to convert each internal Array > separately to an Array of doubles: > > matrix [ 1.1, 1.2, 1.3, 1.4 ].to_java(Java::double), [ 2.1, 2.2, 2.3, > 2.4 ].to_java(Java::double), [ 3.1, 3.2, 3.3, 3.4 > ].to_java(Java::double) ].to_java > > While that takes care of the internal arrays, the result of this expression > is STILL an Array of Objects, not an Array of Arrays. > > Which brings us to the question (which one of the JRuby gurus will > hopefully answer), how do we specify an Array as a parameter in to_java? > > BTW, the case of String is special. JRuby does some internal magic to > convert Ruby Strings to Java Strings, but that's just JRuby doing its thing. > There is no such similar thing for Arrays which is why you need to jump > through the to_java hoops to convert between them. > > Hope this helps, > -Mario. > --001636b2b8e7b642cb0475e94a14--