Passing JRuby to Java

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?


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:

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.

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.