Skip to content

Instantly share code, notes, and snippets.

@gertig
Forked from seanlilmateus/gist:3187192
Last active December 21, 2015 03:38
Show Gist options
  • Save gertig/6243272 to your computer and use it in GitHub Desktop.
Save gertig/6243272 to your computer and use it in GitHub Desktop.

Converting Obj-C blocks to RubyMotion

A guide on how to translate Objective-C blocks into Ruby blocks.

Ruby Lambda and Proc Syntaxes

block = lamda { |param|  ... }
block = lamda do |param|
	...
end

block = -> param { ... }
block = -> param do
	...
end

block = Proc.new{ |param| ... }
block = Proc.new do |name|
	....
end

block = proc { ... }
block = proc do |name|
  ...
end

Blocks with 0 arguments

+ animateWithDuration:(NSTimeInterval)duration
	animations:(void (^)(void))animations

this method takes two arguments duration and animations, where animations is a block (lambda) that takes no arguments

Objective-C

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

Ruby

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

Blocks with 1 argument

animateWithDuration:(NSTimeInterval)duration 
	 animations:(void (^)(void))animations 
	 completion:(void (^)(BOOL finished))completion

this method takes three arguments duration, animations and completion where animations and completion are blocks. the animations block does not take an argument, but completion does.

Objective-C

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

Ruby

# Using the Ruby 1.9 Lambda sytax
UIView.animateWithDuration(0.2,
     animations:-> { view.alpha = 0.0 },
     completion:-> finished { view.removeFromSuperview })

Since we don't use the finished variable, we could also do it in Ruby this way:

# Using the Ruby 1.9 Lambda sytax
UIView.animateWithDuration(0.2,
     animations:-> { view.alpha = 0.0 },
     completion:-> _ { view.removeFromSuperview })

Blocks with 2 arguments

enumerateObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block

This NSSet method is an enumeration method that takes a block with two arguments.

Objective-C

NSSet *aSet = [NSSet setWithObjects: @"X", @"Y", @"Z", @"Pi", nil];
NSString *aString = @"z";
 
[aSet enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
     if ([obj localizedCaseInsensitiveCompare:aString]==NSOrderedSame) {
          NSLog(@"Object Found: %@", obj);
          *stop = YES;
     }
} ];

Ruby

the_set = NSSet.setWithObjects("X", "Y", "Z", "Pi", nil)
the_str = "z"

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

Blocks with 3 arguments

(void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts 
	usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

This NSArray method uses Block-based Enumeration

Objective-C

NSArray *anArray = [NSArray arrayWithObjects:@"A", @"B", @"D", @"M", nil];
NSString *string = @"c";
 
[anArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){
     if ([obj localizedCaseInsensitiveCompare:string] == NSOrderedSame) {
          NSLog(@"Object Found: %@ at index: %i",obj, index);
          *stop = YES;
     }
} ];

Ruby

the_array = ["A", "B" ,"D", "C", "M"]
the_str = "c"

the_array.enumerateObjectsUsingBlock(-> obj, index, stop {
    if (obj.localizedCaseInsensitiveCompare(the_str) == NSOrderedSame)
        NSLog("Object Found: %@ at index: %@", obj, index)
		stop.assign(true)
	end
})

One last Ruby Example

strings = ["string 1", "String 21", "string 12", "String 11", "String 02"]

comparison_opts = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch
current_locale = NSLocale.currentLocale

result = strings.sortedArrayUsingComparator(lambda do |first, second|
  first_range = NSMakeRange(0, first.length)
  first.compare(second, options:comparison_opts, range:first_range, locale:current_locale)
end
)
NSLog("finderSortArray: %@", result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment