Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created December 15, 2010 01:53
Show Gist options
  • Save vaskoz/741515 to your computer and use it in GitHub Desktop.
Save vaskoz/741515 to your computer and use it in GitHub Desktop.
Want to use Ruby functionality in Java? Compile it into a JAR
import org.jruby.*;
import org.jruby.runtime.ThreadContext;
public class JavaUsingRubyLib {
public static void main(String[] args) {
Service s = new Service();
Ruby rt = Ruby.getGlobalRuntime();
ThreadContext ctx = rt.getCurrentContext();
RubyArray response1 = (RubyArray) s.send_msg("Test message 1");
RubyArray response2 = (RubyArray) s.send_msg(2000);
RubySymbol sym2 = RubySymbol.newSymbol(rt, ":testSymbol");
RubyArray response3 = (RubyArray) s.send_msg(sym2);
RubyString joinString = RubyString.newString(rt, "->");
System.out.println(response1.join(ctx, joinString));
System.out.println(response2.join(ctx, joinString));
System.out.println(response3.join(ctx, joinString));
}
}
public class Service extends RubyObject {
private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
private static final RubyClass __metaclass__;
static {
String source = new StringBuilder("class Service\n" +
" attr_accessor :count # This doesn't generate getters/setters in Java class\n" +
"\n" +
" def count(count)\n" +
" @count = count\n" +
" end\n" +
"\n" +
" def count\n" +
" @count ||= 0\n" +
" end\n" +
"\n" +
" def send_msg(msg=\"this is the default message\")\n" +
" puts \"Sending #{msg}\"\n" +
" [msg, self.count += 1]\n" +
" end\n" +
"end\n" +
"").toString();
__ruby__.executeScript(source, "service.rb");
RubyClass metaclass = __ruby__.getClass("Service");
metaclass.setRubyStaticAllocator(Service.class);
if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: Service");
__metaclass__ = metaclass;
}
/**
* Standard Ruby object constructor, for construction-from-Ruby purposes.
* Generally not for user consumption.
*
* @param ruby The JRuby instance this object will belong to
* @param metaclass The RubyClass representing the Ruby class of this object
*/
private Service(Ruby ruby, RubyClass metaclass) {
super(ruby, metaclass);
}
/**
* A static method used by JRuby for allocating instances of this object
* from Ruby. Generally not for user comsumption.
*
* @param ruby The JRuby instance this object will belong to
* @param metaclass The RubyClass representing the Ruby class of this object
*/
public static IRubyObject __allocate__(Ruby ruby, RubyClass metaClass) {
return new Service(ruby, metaClass);
}
/**
* Default constructor. Invokes this(Ruby, RubyClass) with the classloader-static
* Ruby and RubyClass instances assocated with this class, and then invokes the
* no-argument 'initialize' method in Ruby.
*
* @param ruby The JRuby instance this object will belong to
* @param metaclass The RubyClass representing the Ruby class of this object
*/
public Service() {
this(__ruby__, __metaclass__);
RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "initialize");
}
public Object count(Object count) {
IRubyObject ruby_count = JavaUtil.convertJavaToRuby(__ruby__, count);
IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "count", ruby_count);
return (Object)ruby_result.toJava(Object.class);
}
public Object count() {
IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "count");
return (Object)ruby_result.toJava(Object.class);
}
public Object send_msg(Object msg) {
IRubyObject ruby_msg = JavaUtil.convertJavaToRuby(__ruby__, msg);
IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "send_msg", ruby_msg);
return (Object)ruby_result.toJava(Object.class);
}
}
class Service
attr_accessor :count # This doesn't generate getters/setters in Java class
def count(count)
@count = count
end
def count
@count ||= 0
end
def send_msg(msg="this is the default message")
puts "Sending #{msg}"
[msg, self.count += 1]
end
end
# compile with: jrubyc --javac service.rb
@vaskoz
Copy link
Author

vaskoz commented Dec 15, 2010

service.rb is compiled into Service.java/Service.class by "jrubyc --javac service.rb"
compile JavaUsingRubyLib.java with: javac -cp ~/.rvm/rubies/jruby-1.5.6/lib/jruby.jar:. JavaUsingRubyLib.java
run all with: java -cp ~/.rvm/rubies/jruby-1.5.6/lib/jruby.jar:. JavaUsingRubyLib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment