【问题标题】:Pass a Ruby block to a Java method将 Ruby 块传递给 Java 方法
【发布时间】:2014-01-11 03:06:36
【问题描述】:

我需要我的 JRuby 方法来接收 Java 方法中发生的事情的报告并采取相应的行动:

def my_long_running_method
  # ... do stuff ...

  jObject.count_all_disk_files do |number_of_files|
    stream.write "#{number_of_files} files found so far."
  end

  # ... do more stuff ...
end

我看到了this question,但我不明白我需要做什么。

我已经了解 Ruby 很长时间了,还对 Java 有一点了解。这个项目需要 JRuby,因为旧的 Java 代码需要与新的 Ruby 代码集成。

【问题讨论】:

    标签: java ruby-on-rails ruby jruby block


    【解决方案1】:

    如需完整参考,请参阅Calling Java From JRuby wiki page,特别是关于closure conversion 的部分。对您的问题的简短回答是您的 Java 方法 count_all_disk_files 将需要接受具有单个方法的接口(“功能接口”)。然后 JRuby 将能够使用块中的代码自动实现该接口。

    【讨论】:

    • 这对我帮助很大,甚至超过了官方文档。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 2014-01-05
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多