Forum: JRuby Convert Ruby Array to Java Array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marcin Piórkowski (Guest)
on 2008-10-17 13:17
(Received via mailing list)
Hi everyone,

Recently I would like to convert Ruby array to Java array with 'double'
elements. I use syntax:

*ruby_array = [[1,2],[3,4]]*
*java_array = ruby_array.to_java :Double*

which return: *"java.lang.reflect.Array:-2:in `set':
java.lang.IllegalArgumentException: array element type mismatch..."*

When I use: "j*ava_array = ruby_array.to_java :String*", it works fine.

Can anyone help me of that problem?

I use: JRuby 1.1.4, Windows XP

Thanks a lot :)

Marcin
Ola B. (Guest)
on 2008-10-17 13:27
(Received via mailing list)
Marcin Piórkowski wrote:
>
> When I use: "j/ava_array = ruby_array.to_java :String/", it works fine.
>
> Can anyone help me of that problem?
>
> I use: JRuby 1.1.4, Windows XP
>
> Thanks a lot :)
>
> Marcin
Well, the problem here is that ruby_array is actually an array of
arrays. I think you would have to do something like this:

/ruby_array = [[1,2],[3,4]]/
//java_array = ruby_array.map{|arr| arr.to_java :double}.to_java
Java::double[][]

Not sure if this is exactly correct, actually...

--
 Ola B. (http://olabini.com)
 JRuby Core Developer
 Developer, ThoughtWorks Studios (http://studios.thoughtworks.com)
 Practical JRuby on Rails (http://apress.com/book/view/9781590598818)

 "Yields falsehood when quined" yields falsehood when quined.



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Marcin Piórkowski (Guest)
on 2008-10-17 14:11
(Received via mailing list)
Thanks Ola for quickly answer :)

Hmm, when I use what you wrote I got an error: "'*to_java': wrong
argument
type #<Class:01xc10de0> (expected Array)*"

Maybe is there any diffrent method to create a matrix in JRuby and after
that use it in JAMA (Java Matrix Package).

Thanks for help :)

Marcin

On Fri, Oct 17, 2008 at 11:23 AM, Ola B. <removed_email_address@domain.invalid> 
wrote:

>>  Can anyone help me of that problem?
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>


--
Pozdrawiam
Marcin Piórkowski
Kom.: +48 506 606 648
http://www.piorkowski.in

"Jeżeli chcesz zmian, zacznij od siebie"
This topic is locked and can not be replied to.