Skip to content

Instantly share code, notes, and snippets.

@Yasushi
Created March 18, 2009 07:42
Show Gist options
  • Save Yasushi/80990 to your computer and use it in GitHub Desktop.
Save Yasushi/80990 to your computer and use it in GitHub Desktop.
import java.util._
import org.jruby._
import org.jruby.javasupport._
import org.jruby.runtime._
import org.jruby.runtime.builtin._
import org.jruby.runtime.callback._
implicit def javaStr2Ruby(s:String)(implicit runtime: Ruby) =
RubyString.newUnicodeString(runtime, s)
def callHoge(clazz:RubyClass)(implicit runtime: Ruby) = {
clazz.allocate.callMethod(runtime.getCurrentContext, "hoge", "a=1")
clazz.allocate.callMethod(runtime.getCurrentContext, "hoge", "p a")
}
implicit val runtime = JavaEmbedUtils.initialize(new ArrayList[String])
val myClass = runtime.defineClass("MyClass", runtime.getObject, runtime.getObject.getAllocator)
myClass.defineFastMethod("hoge", new Callback() {
override def execute(self:IRubyObject, args:Array[IRubyObject], block:Block) = {
val context = self.getRuntime.getCurrentContext
self.callMethod(context, "eval", args, block)
}
override def getArity = Arity.ONE_REQUIRED
})
callHoge(myClass)
runtime.evalScript("class MyClass2; def hoge(expr); eval(expr); end; end")
callHoge(runtime.getClass("MyClass2"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment