Forum: JRuby Passing JRuby to Java

6b611063e1aec3f09bdca7e406b3a765?d=identicon&s=25 Ray H. (ray_h)
on 2013-12-03 01:05
I am wondering it if it is practical to pass JRuby objects to java.  The
goal is to insert JRuby objects (actually Mongoid retrieved data/
classes) into
Drools.  Anyhow I wanted to see what they looked like in Java.  So I
wrote a quick Java program design to do reflection and inspect the
object passed to it.   I then retrieved an object and passed it to my
Java program.  Unfortunately the class name per Java is
org.jruby.RubyObject and the package is org.jruby.  That means that Java
sees none of the Ruby class name info.

I thought I saw a toJava method using reflection, but it is not known
to JRuby (i.e., invalid method).    I thought about serializing to Json
and then back one
in Java, but that is a bit messy.  Any other thoughts?

Ray
2c0c4cf3ccc8da22f7c3b9586ce1cd70?d=identicon&s=25 Christian MICHON (Guest)
on 2013-12-03 10:49
(Received via mailing list)
I would advise to use Marshal (from ruby/jruby) to serialize in and out
instead of json. This is not messy and it will work.

I'm doing this to store jruby objects with mapdb.

Here's how I do it:
https://github.com/cmichon/jruby-mapdb/blob/master...
6b611063e1aec3f09bdca7e406b3a765?d=identicon&s=25 Ray H. (ray_h)
on 2013-12-03 15:24
Christian,  I wanted to get a bit more info about the Marshal class, but
cannot seem to find it.  I searched in the JRuby API but only saw the
MarshalStream class.

Like I said, I need to pass the objects to Drools and Drools need to
recognize them as a special named class for the "facts" to work.  That
was why I created a Java class to inspect an object and then passed a
JRuby mongoid/moped object.  JRuby properly see it as an object of type
"Record" or whatever I have called it, but my Java program saw the class
as "RubyObject".

Drools is a rules engine that responds to facts that are actually Java
classes with specific values.  So if for example, I have customer data
in a class but Drools not see the class as being "Customer", then it
will not trigger processing.  The class types have to match as well as
the method calls.

Ray
2c0c4cf3ccc8da22f7c3b9586ce1cd70?d=identicon&s=25 Christian MICHON (Guest)
on 2013-12-03 16:27
(Received via mailing list)
I see: if you want to inspect jruby object with some java tools,
Marshalling is not the way.

If you need to store/retrieve jruby objects in java without inspecting
them, Marshalling is a useful trick to remember.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.