Marin's internet home

https://mar.in

Adding some Ruby sugar to ObjectiveC

I always liked Ruby’s iterators and number objects.

1.upto(10) { |i| print i }
# or
5.times { print "Hello!\n" }
# or even
[:one, :two, :three].each { |number| print number }

Recently, Apple has introduced new llvm with new literals including NSNumber ones, and that opened lots of potential for code improvements.
Today, while writing some code for Domainchy, one idea came to my mind…

[@3 times:^{
  NSLog(@"Hello!");
}];
// Hello!
// Hello!
// Hello!
[@3 timesWithIndex:^(int index) {
  NSLog(@"Another version with number: %d", index);
}];
// Another version with number: 0
// Another version with number: 1
// Another version with number: 2

[@1 upto:4 do:^(int numbah) {
  NSLog(@"Current number.. %d", numbah);        
}];
// Current number.. 1
// Current number.. 2
// Current number.. 3
// Current number.. 4

[@7 downto:4 do:^(int numbah) {
  NSLog(@"Current number.. %d", numbah);        
}];
// Current number.. 7
// Current number.. 6
// Current number.. 5
// Current number.. 4

Since I’ve already implemented the Ruby-esque -each method in Objective-Record (because -enumerateObjectsWithIndex: is kind of too verbose),

NSArray *cars = [@"Testarossa", @"F50", @"F458 Italia"]; 

[array each:^(id object) {
    NSLog(@"Car: %@", object); 
}];
// Car: Testarossa
// Car: F50
// Car: F458 Italia

[array eachWithIndex:^(id object, int index) {    
    NSLog(@"Car: %@ index: %i", object, index); 
}];
// Car: Testarossa index: 0
// Car: F50 index: 1
// Car: F458 Italia index: 2

id object = array.first; // Testarossa
id object = array.last;  // 458 Italia

…maybe it’ll be a good idea to separate this in another pod.

pod ObjectiveSugar
You’re welcome.