Created
March 18, 2009 07:42
-
-
Save Yasushi/80990 to your computer and use it in GitHub Desktop.
scala version http://gist.github.com/80274
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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