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.

Hi,
I've ran into a little problem while starting off with JRuby embedding.
I'm using the following code fragment for getting the RubyClass of a
class:
String[] paths = new String[]{"."};
Ruby rt = JavaEmbedUtils.initialize(Arrays.asList(paths));
rt.executeScript(FileUtils.readFileToString(new File("bar.rb")),
"bar.rb");
System.out.println("cl: "+rt.getClass("Foo::Bar"));
Is the correct way to do it?
The problem is that Ruby.getClass method call returns null.
If I rename class Foo::Bar to Bar the method call returns a non-null
value.
I'm working with a code base that uses this naming convention for
subclasses and I can't change the convention.
I'm using JRuby v1.6.7
marko

On Mon, Jun 4, 2012 at 11:45 AM, Yoko Harada <yokolet@gmail.com> wrote:
> import org.jruby.embed.PathType;> container.runScriptlet(PathType.ABSOLUTE, filename);> Object obj = container.runScriptlet("Foo::Bar");> System.out.println(obj);> }>> public static void main(String[] args) {> new FooBar();> }> }
Hi,
Thanks.
I read the RedBridge wiki page you mentioned and tried migrating my
code to use ScriptingContainer.
However, I'm having some problems figuring out how to use the newer API.
For example previously I used to instantiate Ruby object from Java
with the following code:
IRubyObject[] args = new IRubyObject[] {
// constructor arguments here
};
IRubyObject obj =
ruby.getClass("Foo").newInstance(ruby.getCurrentContext(), args,
Block.NULL_BLOCK);
How do I do this with ScriptingContainer?
Also, some parts of the API such as RubyHash.newHash seems to require
a Ruby object as a parameter.
ps. i noticed that i can get the class Foo::bar by calling
rt.getClass("Foo").getClass("Bar");
marko

Hi,
Thanks a lot for your help, Yoko!
I managed to migrate my code to use theScriptingContainer API and it
seems to be working fine.
I'm planning on embedding JRuby in a multi-threaded (servlet
container) environment and I still have a few questions related to
this regarding thread-safety and usage patterns.
Here're some code snippets and questions:
// Ruby rt + ScriptingContainer are initialized only once at startup
ScriptingContainer ruby = new
ScriptingContainer(LocalContextScope.CONCURRENT);
ruby.setLoadPaths(getRubyloadPaths());
can the ScriptingContainer instance be shared between threads or
should one be instantiated per thread?
what happens if ruby.setLoadPaths() is invoked after other threads
havestarted to use the ScriptingContainer?
when should Ruby libraries be loaded (require 'foo')? Can they be
loaded at any time? Will they be available to all threads? Are any
thread-safetyissues involved?
// each request is processed by a different thread.
// a new Ruby service instance is created to process each request.
// create/convert input parameters
Converter converter = new Converter(ruby);
Object[] args = new Object[] {
ruby.runScriptlet(":symbolX"),
converter.deepConvert(eventData) // see Converter class below
};
// create service instance
Object srv = ruby.runScriptlet(serviceName);
IRubyObject service = ruby.callMethod(srv, "new", args,
IRubyObject.class);
// invoke service
ruby.callMethod(service, "process_event", Object.class);
//----
public class Converter {
// ...
public IRubyObject deepConvert(Object obj) {
// method implementation converts a complex Java data structure to
JRuby class instances like this:
RubyHash h = ruby.callMethod(ruby.runScriptlet("Hash"), "new",
RubyHash.class);
h.put("foo", "bar");
// ...
return h;
}
}
Does this look like a valid way to use the JRuby embedding API?
regards,
marko