如何将 Jruby proc/block/closure 返回到 java 并调用它
我希望能够将块从嵌入式 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以直接嵌入,请使用 JavaEmbedUtils 并在定义您的 proc/block/closure 的 ruby 代码字符串上调用 eval,或者让您的 ruby 代码实现一个接口,其中某些方法返回您的 proc/block/closure。令人难以置信的设计示例,将以下两个数字相加:
SampleApp.java
SomeInterface.java
SomeInterfaceImpl.rb
这就是您可以测试的方法:
参考文献:
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
SomeInterface.java
SomeInterfaceImpl.rb
And this is how you can test:
References:
https://github.com/jruby/jruby/wiki/DirectJRubyEmbedding
http://tommy.chheng.com/2010/06/20/call-a-jruby-method-from-java/