JRuby:Java中的命令模式与Ruby块:为什么它有效? [英] JRuby: command pattern in Java with Ruby block: why does it work?

查看:210
本文介绍了JRuby:Java中的命令模式与Ruby块:为什么它有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何将Java库与Ruby代码集成,并提出以下问题。

I am learning how to integrate Java library with Ruby code and come to the following question.

我有一个用Java实现的命令模式,如下所示:

I have a command pattern implemented in Java, as follows:

public interface Command {
  public String execute(String param);
}

public class CommandRunner {

  public String run(Command cmd, String param) {
    return cmd.execute(param)+" [this is added by run method]";
  }
}

当我将其导入JRuby程序时,我可以实现Ruby respond_to的类? :使用一个参数执行并将其传递给 CommandRunner.new.run 。这很有效。

When I import that to JRuby program I can implement Ruby class that respond_to? :execute with one parameter and pass it to the CommandRunner.new.run. That works and that's clear.

但我也可以这样做:

def put_through_runner(param, &block)
  CommandRunner.new.run block, param
end

p = put_through_runner "through method" do |param|
  "Cmd implementation in block, param: #{param}"
end

puts p

而不是传递给Java CommandRunner 实现执行方法的对象I传递一段代码,但没有实现该方法。它的工作原理是:调用块就像执行执行方法一样!怎么可能?当JRuby将它传递给Java时,它对块做了什么?如果我在Ruby中实现了 CommandRunner ,则上述代码将无效。

Instead of passing to Java CommandRunner an object implementing the execute method I pass it a block of code, that does not implement the method. And It works: calls the block as if it was implementation of the execute method! How is that possible? What does JRuby do with the block when passing it to Java? If I had the CommandRunner implemented in Ruby the above code would not work.

推荐答案

这有效的原因是一个名为'封闭转换'的功能(查看文档这里)。会发生的情况是,您传递的块被转换为 Proc 对象,该对象具有一个代理,该代理为对象上调用的任何方法调用块中的代码。

The reason that this works is a feature called 'closure conversion' (see docs here). What happens is that the block you pass is converted into a Proc object with a proxy that invokes the code in the block for any method that is called on the object.

这篇关于JRuby:Java中的命令模式与Ruby块:为什么它有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆