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

发布于 2024-11-26 22:53:21 字数 87 浏览 1 评论 0原文

我希望能够将块从嵌入式 jruby 实例返回到常规 java。我的java代码应该能够传递所需的参数(假设它知道正确的数量),然后接收结果。任何样品将不胜感激。

I would like the ability to return blocks from an embedded jruby instance to regular java. My java code should be able to pass the required parameters (lets assume it knows the right arity) and then receive the result. Any samples would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-12-03 22:53:21

如果您可以直接嵌入,请使用 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/从-java调用a-jruby-method-

If you can do direct embedding, use JavaEmbedUtils and either call eval on a string of ruby code that defines your proc/block/closure or have your ruby code implement an interface where certain methods return your proc/block/closure. Incredibly contrived example to sum two numbers below:

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

And this is how you can test:

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

References:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文