logo       

Re: [jruby-user] JRuby array to Java array: msg#00202

lang.jruby.user

Subject: Re: [jruby-user] JRuby array to Java array

Yup, that did the trick.  Is this documented anywhere?  If not, it seems like a good thing to add to the wiki.  I could put it under the "Calling Java from JRuby" section, unless it's already somewhere else and I missed it.

On 21/11/2007, Bill Dortch <bill.dortch@xxxxxxxxx> wrote:
This should work:

tup = OtpErlangTuple.new( [firstErlObject,
secondErlObject].to_java(OtpErlangObject) )

-Bill

On Nov 21, 2007 11:46 AM, tsuraan <tsuraan@xxxxxxxxx > wrote:
> My first two attempts got rejected, so hopefully this will get through...
>
> I'm using the Erlang JInterface library
> ( http://www.erlang.org/doc/apps/jinterface/index.html ) to communicate
> between my ruby code and my erlang code. One of the key objects in this
> library is the OtpErlangTuple, which takes an array of OtpErlangObject
> instances as the argument to its constructor. Looking through the Jira bug
> reports, I found that you can make a JRuby java array like so:
>
> ary = OtpErlangObject[2].new
>
> And then assignment is just like assignment to any other array, and the
> tuple can be constructed with ary as its constructor argument. What I'm
> wondering is why this can't be done in a more ruby-ish fashion. What I want
> to be able to do is:
>
> tup = OtpErlangTuple.new [ firstErlObject, secondErlObject, etc ]
>
> but the JRuby array is not a Java array, so this gives a NameError. JRuby
> arrays has a to_java method, which converts the org.jruby.RubyArray into a
> java.lang.Object, but that's still not a valid array. I've also tried
>
> tup = OtpErlangTuple.new OtpErlangObject[2].new( [ firstErlObject,
> secondErlObject ] )
>
> but that that gives the puzzling message "NativeException:
> java.lang.IllegalArgumentException: array element type mismatch." All the
> elements in the JRuby array are OtpErlangObjects, so I don't know what the
> type mismatch is. Is there a clean way to convert a JRuby array into a Java
> one, or do I just have to make the array using the Object[size].new and
> assign all the elements by hand?
>
>

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

    http://xircles.codehaus.org/manage_email


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise