我想看看这个 Java code会在 JRuby 中查找:
ParseQuery query = new ParseQuery("MyClass");
query.getInBackground(myId, new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
objectWasRetrievedSuccessfully(object);
} else {
objectRetrievalFailed();
}
}
});
对我来说最大的困惑是匿名内部类。这是我最好的第一个猜测:
query = ParseQuery.new("GameScore")
query.getInBackground("xWMyZ4YEGZ", Class.new(GetCallback) do
def done(object, e)
# ...
end
end.new)
最佳答案
在 JRuby 中表达它的语法看似简单。 JRuby 有一个称为“闭包转换”的特性,其中传递给方法的闭包可以转换为适当的 Java 接口(interface)。来自JRuby docs :
This not only works for event listeners or Runnable, but basically for any interface. When calling a method that expects an interface, JRuby checks if a block is passed and automatically converts the block to an object implementing the interface.
因此,您的代码如下所示:
query.in_background 'xWMyZ4YEGZ' { |object, e|
# do stuff
}
'calling Java from JRuby' JRuby wiki 上的页面是解决此类问题的良好资源。
关于java - 将 Parse Java SDK 翻译成 JRuby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963400/