Use already instanciated Java class in Jirb

Hi!

I’m trying to integrate the JRuby console JIRB, inside an existing
JFrame based application.

It works correctly and I can instanciate and use Java classes, from
“irb>” console.

Now I am trying use an application class that is already instanciated.

Is it possible to pass to Jirb a reference to that instance ?

All examples I manage do search for, always create a new instance inside
“jirb>”. I don’t want to do that.

Thanks,

Eduardo

On Tue, Sep 27, 2011 at 8:23 AM, Eduardo R. [email protected]
wrote:

Is it possible to pass to Jirb a reference to that instance ?
It’s definitely possible, though the specifics depend upon which
embedding API you’re using.

Here’s an example I have using the JSR 223 embedding approach:

Note that after putting this value in the scope with the name “foo”,
it’s available within Ruby code as a global variable, so must be
accessed as $foo.

Hello!

On Tue, Sep 27, 2011 at 9:34 AM, Anthony J. [email protected]
wrote:

Is it possible to pass to Jirb a reference to that instance ?

It’s definitely possible, though the specifics depend upon which
embedding API you’re using.

Here’s an example I have using the JSR 223 embedding approach:

Cool!

Note that after putting this value in the scope with the name “foo”,
it’s available within Ruby code as a global variable, so must be
accessed as $foo.

This is a default behavior. You can change to other one really natural
to Ruby.
See, RedBridge · jruby/jruby Wiki · GitHub

Eduardo:
You can choose to use ScriptingContainer. I guess you don’t use other
languages from the same Java code. In such a case, ScriptingContainer
would be easier to use.

-Yoko

Hi!

Thanks for the replies but I still did not managed to make it work.
I didn’t explain exactly what I am trying to accomplish.

I found an example based on java class TextAreaReadline.
This shows a console inside a Java Swing JFrame.
From the example I only have two class instances available: Ruby and
RubyInstanceConfig. From the Javadoc, I could not interpret if some of
its method could help me.

Without using JSR 223 and BSF approaches, how do I manage to pass Java
objects to the TextAreaReadline “console” ?

I found “org.jruby.javasupport.JavaEmbedUtils.java” class also. Does it
help?

Thanks,

Edu

Hi,

On Tue, Oct 4, 2011 at 10:22 AM, Eduardo R. [email protected]
wrote:

runtime.getGlobalVariables().set(GlobalVariable.variableName(“rbComponent”),
rbComponent);

You can do like above, but is not recommended.
Do you know JRuby has embedding API? If not,
RedBridge · jruby/jruby Wiki · GitHub will help you how to do
that.

Hope this helps,
-Yoko

Hi!

Found what I was looking for.
I only needed to call these lines:

Component myComponent = …; // this is my object

IRubyObject rbComponent =
JavaEmbedUtils.javaToRuby(runtime, myComponent);

runtime.getGlobalVariables().set(GlobalVariable.variableName(“rbComponent”),
rbComponent);

Inside JRuby console, i could access that object:

irb(main):002:0> $rbComponent
#Java::PtEfacecSeAutComponentsEvtsuite::EvtSuiteComponent:0x10dba0d