Forum: JRuby Problem when calling from Ruby->Java->Ruby

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.
768d87a25403cb77f2d5efc4a14f1fa5?d=identicon&s=25 Gary Lin (Guest)
on 2009-02-20 07:22
(Received via mailing list)
Hi,

I am trying to have jruby.bat invoke my ruby script which then calls a
Java function.  Inside my java function, I want to be able to call back
my ruby script (for example, invoke method on some ruby object that was
created initially by the ruby script).  I wonder how I can accomplish
this?

I follow the instruction and did the following:
===========================================
[ruby script]

class Tool
   def some_ruby_func
       puts :foo.hash
   end
....
end

puts :foo.hash
a = com.foo.Foo.new
a.foo(Tool.new)
--------------------------------------------------------------
[java code]
public class Foo {
    public void foo(RubyObject o) throws Exception
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("jruby");
        engine.put("tool", o);
        engine.eval("$tool.some_ruby_func");
    }
}
===========================================
As the result, the first "puts :foo.hash" will end up be different than
the second output of the "puts :foo.hash".  This causes a huge problem
when I have hash that uses symbol as the key.
I guess this cause of this is because I am printing the symbol via 2
different ruby runtime engine instance?  If so, I wonder if there is
anyway I can get the "current" running script engine runtime instead of
having to create a new instance of it?

Appreciated for your help.

Thanks,
--Gary

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

    http://xircles.codehaus.org/manage_email
526d60de6472502bb570a9df2842b33b?d=identicon&s=25 Nick Sieger (Guest)
on 2009-02-20 15:44
(Received via mailing list)
On Fri, Feb 20, 2009 at 12:21 AM, Gary Lin <glin@employees.org> wrote:
>
> --------------------------------------------------------------
> As the result, the first "puts :foo.hash" will end up be different than the
> second output of the "puts :foo.hash".  This causes a huge problem when I
> have hash that uses symbol as the key. I guess this cause of this is because
> I am printing the symbol via 2 different ruby runtime engine instance?  If
> so, I wonder if there is anyway I can get the "current" running script
> engine runtime instead of having to create a new instance of it?
>
> Appreciated for your help.

Today we don't have any notion of "current" runtime or a
straightforward way of getting at it from bare Java code. But since
you're passing a RubyObject you can just call the method on it
directly:

[java code]
public class Foo {
  public void foo(RubyObject o) throws Exception {
    Ruby runtime = o.getRuntime();
    o.callMethod(runtime.getCurrentContext(), "some_ruby_func");
  }
}

Cue the obligatory mention of coupling to JRuby-specific APIs, but if
you're willing to live with that, the above approach should work fine.

/Nick

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

    http://xircles.codehaus.org/manage_email
This topic is locked and can not be replied to.