Skip to content

Convertion of ObjC blocks to Ruby #3

@seanlilmateus

Description

@seanlilmateus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions