Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate Parse Java SDK to JRuby

I'd like to see how this Java code would look in 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();
     }
   }
 });

The biggest part of confusion for me is the anonymous inner class. This is my best first guess:

query = ParseQuery.new("GameScore")
query.getInBackground("xWMyZ4YEGZ", Class.new(GetCallback) do
  def done(object, e)
    #  ...
  end 
end.new)

Update: Edited based on this: http://www.ruby-forum.com/topic/188599#823271

like image 303
user94154 Avatar asked Aug 12 '26 12:08

user94154


1 Answers

The syntax for expressing this in JRuby is deceptively simple. JRuby has a feature called 'closure conversion' where a closure passed to a method can is converted into the appropriate Java interface. From the 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.

So, your code would look like:

query.in_background 'xWMyZ4YEGZ' { |object, e|
  # do stuff
}

The 'calling Java from JRuby' page on the JRuby wiki is a good resource for problems like these.

like image 139
Paul Morie Avatar answered Aug 15 '26 01:08

Paul Morie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!