title: Calling Ruby method from Java
body: <p>I have a Java application that depends on the result from a ruby script. The issue I'm facing is that this ruby script is called many times which requires loading the script's libraries every time it's executed.</p>
  
  <p>I'm not familiar with Ruby, but in my search I've come across JRuby. Since it interprets ruby code and runs it on the JVM, my initial thought was "great, I can just compile the ruby script to .class files package it up and interact with it like regular java objects". This would avoid the overhead that came with loading the libraries each time I executed the ruby script.</p>
  
  <p>After looking deeper into jruby, I'm understanding it doesn't work this way. So to do what I want I can use JRuby's JavaEmbedUtils to</p>
  
  <ol>
  <li>Load a Ruby runtime object with my ruby scripts</li>
  <li>Create a receiver object that essentially knows how to handle and respond to my script.</li>
  <li>Invoke the desired method from script.</li>
  </ol>
  
  <p>This is what I had in mind (I'll be testing soon)</p>
  
  <pre><code>// 1  
  List&lt;String&gt; paths = new ArrayList&lt;&gt;();  
  paths.add(".");  
  Ruby runtime = JavaEmbedUtils.initialize(paths);
  
  // 2  
  String script = "/path/to/script.rb";  
  IRubyObject recvr = JavaEmbedUtils.newRuntimeAdapter().eval(runtime, script);
  
  // 3 Call this many times   
  JavaEmbedUtils.invokeMethod(runtime, receiver, "method", null, null);
  </code></pre>
  
  <p>Is my understanding correct in that this approach allows me to use the contents of the script many times while loading the script's libraries once? Is there an alternate or more JRuby way of doing what i'm looking for?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So I tested something similar to what Eugene suggested (a) i.e. ScriptingContainer and compared that to calling the script using (b) java.lang.Runtime for a total of 30 runs. </p>
  
  <ol>
  <li>On average, (a) was about 8x faster (b)</li>
  <li>Initializing (a)'s runtime took about 7 seconds</li>
  <li>The initial run for both (a) &amp; (b) took 40x &amp; 150-250x longer than following runs.</li>
  <li>(a)'s initial run was 11x faster than (b)s</li>
  </ol>