SublimeObjC2RubyMotion icon indicating copy to clipboard operation
SublimeObjC2RubyMotion copied to clipboard

Convertion of ObjC blocks to Ruby

Open seanlilmateus opened this issue 11 years ago • 1 comments

I would be nice to have ObjC blocks translation as promised, here are the snippets e.g:

Objective C blocks without params

[UIView animateWithDuration:0.2
                             animations:^{view.alpha = 0.0;}]

to ruby

UIView.animateWithDuration(0.2, animations: -> { view.alpha = 0.0 })

Objective-C blocks with one params

[UIView animateWithDuration:0.2
                             animations:^{view.alpha = 0.0;}
                             completion:^(BOOL finished){ [view removeFromSuperview]; }];

to ruby

UIView.animateWithDuration(0.2,
     animations:-> { view.alpha = 0.0 },
     completion:-> finished { view.removeFromSuperview })

Objective-C Blocks with two arguments

[aSet enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
     if ([obj localizedCaseInsensitiveCompare:aString]==NSOrderedSame) {
          NSLog(@"Object Found: %@", obj);
          *stop = YES;
     }
} ];

to ruby using different lambda syntax:

the_set.enumerateObjectsUsingBlock(lambda do |obj, stop|
  if obj.localizedCaseInsensitiveCompare(the_str) == NSOrderedSame
    NSLog("Object Found: %@", obj)
    stop.assign(true)
  end
end)

and so on

seanlilmateus avatar Apr 11 '13 11:04 seanlilmateus

Hey @seanlilmateus!

I think some of this is done in: https://github.com/kyamaguchi/SublimeObjC2RubyMotion/blob/master/tests/test_block.py

ivanacostarubio avatar May 22 '13 23:05 ivanacostarubio