How to embed JRuby 1.9 into a Java class

20
Aug/09
0

It took awhile longer to figure this out than I would have liked, due to no on-line JavaDocs for JRuby (what’s up with that!?).

But here’s how to embed JRuby 1.9 into a Java class:


import org.jruby.Ruby;
import org.jruby.RubyRuntimeAdapter;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.RubyInstanceConfig;
import org.jruby.CompatVersion;

import java.util.ArrayList;

public class JRubyEmbedded {
  public static void main(String[] args) {
    RubyInstanceConfig ric = new RubyInstanceConfig();
    ric.setCompatVersion(CompatVersion.RUBY1_9);
    Ruby ruby = JavaEmbedUtils.initialize(new ArrayList(), ric);
    RubyRuntimeAdapter rra = JavaEmbedUtils.newRuntimeAdapter();

    System.out.println(rra.eval(ruby, "\"Ruby #{RUBY_VERSION}\""));

    JavaEmbedUtils.terminate(ruby);
  }
}
Filed under: Code
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.