Skip to content

Instantly share code, notes, and snippets.

@mkristian
Created May 7, 2010 11:30
Show Gist options
  • Save mkristian/393304 to your computer and use it in GitHub Desktop.
Save mkristian/393304 to your computer and use it in GitHub Desktop.
package org.codingkata.unit.api;
public interface IKataSolution {
String reply();
}
java_import 'org.codingkata.unit.api.IKataSolution'
class MyKata
java_implements IKataSolution
java_signature 'String reply()'
def reply
# your reply code here
end
end
import org.jruby.Ruby;
import org.jruby.RubyObject;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;
import org.jruby.RubyClass;
import org.codingkata.unit.api.IKataSolution;
public class MyKata extends RubyObject implements IKataSolution {
private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
private static final RubyClass __metaclass__;
static {
String source = new StringBuilder("java_import org.codingkata.unit.api.IKataSolution\n" +
"\n" +
"class MyKata\n" +
" java_implements IKataSolution\n" +
"\n" +
" java_signature \"String reply()\"\n" +
" def reply\n" +
" # your reply code here\n" +
" end\n" +
"end\n" +
"").toString();
__ruby__.executeScript(source, "my_kata.rb");
RubyClass metaclass = __ruby__.getClass("MyKata");
metaclass.setRubyStaticAllocator(MyKata.class);
if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: MyKata");
__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 MyKata(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 MyKata(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 MyKata() {
this(__ruby__, __metaclass__);
RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "initialize");
}
public String reply() {
IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "reply");
return (String)ruby_result.toJava(String.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment