如何将Jruby proc / block / closure返回给java并调用它

时间:2011-07-31 09:16:46

标签: jruby

我希望能够将嵌入式jruby实例中的块返回到常规java。我的java代码应该能够传递所需的参数(假设它知道正确的arity),然后接收结果。任何样品都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

如果你可以直接嵌入,使用JavaEmbedUtils并在一个定义你的proc / block / closure的ruby代码字符串上调用eval,或者让你的ruby代码实现一个接口,其中某些方法返回你的proc / block / closure。令人难以置信的例子总结以下两个数字:

SampleApp.java

import org.jruby.Ruby;
import org.jruby.RubyRuntimeAdapter;
import org.jruby.RubyFixnum;
import org.jruby.RubyProc;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import java.util.ArrayList;

class SampleApp {
        public static void main(String[] args) {

                // Create runtime instance
                Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());

                // Parameters
                RubyFixnum paramA = new RubyFixnum(runtime, 1);
                RubyFixnum paramB = new RubyFixnum(runtime, 2);

                // Runtime eval method
                RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
                RubyProc additionProcFromEval = (RubyProc)evaler.eval(runtime, "Proc.new { |a, b| a + b }");
                printProcResult(runtime, additionProcFromEval, paramA, paramB);

                // Interface implementation method
                SomeInterfaceImpl someImpl = new SomeInterfaceImpl();
                RubyProc additionProcFromInterface = (RubyProc)someImpl.getAdditionProc();
                printProcResult(runtime, additionProcFromInterface, paramA, paramB);

                // Shutdown and terminate instance
                JavaEmbedUtils.terminate(runtime);
        }

        protected static void printProcResult(Ruby runtime, RubyProc proc, RubyFixnum paramA, RubyFixnum paramB) {
                Block block = proc.getBlock();
                ThreadContext threadContext = ThreadContext.newContext(runtime);
                int result = (Integer)block.call(threadContext, paramA, paramB).toJava(new Integer(0).getClass());
                System.out.println("Result = " + result);
        }
}

SomeInterface.java

interface SomeInterface {
    org.jruby.RubyProc getAdditionProc();
}

SomeInterfaceImpl.rb

require 'java'
class SomeInterfaceImpl

        include Java::SomeInterface

        java_signature 'org.jruby.RubyProc getAdditionProc()'
        def getAdditionProc()
                return Proc.new { |a, b| a + b }
        end

end

这就是你可以测试的方式:

javac -classpath jruby.jar SomeInterface.java
jrubyc --javac -cp . SomeInterfaceImpl.rb
javac -cp jruby.jar:. SampleApp.java
java -cp jruby.jar:. SampleApp

参考文献:

https://github.com/jruby/jruby/wiki/DirectJRubyEmbedding

http://tommy.chheng.com/2010/06/20/call-a-jruby-method-from-java/