Take the 2-minute tour ×

I got used to ruby methods like map/collect, uniq for Array, is there any library with common desired methods which will make Objective-C more comfortable for ruby programmer?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

There also is this:


"Implementation of common Ruby Enumerable module methods as a category in Objective-C"

share|improve this answer
Thanks for this. Definitely makes handling enumerable-ish objects in Objective-C suck far less. –  Ben Kreeger Aug 31 '12 at 17:51

Not that I know of, but you could easily harness the power of categories to write your own, here's an example:

#import <objc/objc-runtime.h>

typedef id (^TraversalBlock)(id obj);

@interface NSArray (RubyEnumerable)

  - (NSArray *) map: (TraversalBlock) block;
  - (NSArray *) uniq;


@implementation NSArray (RubyEnumerable)       

  + (void) initialize {
     Method srcMethod = class_getInstanceMethod([NSArray class], @selector(map:));
     IMP srcMethodImplementation = method_getImplementation(srcMethod);
     class_addMethod([NSArray class], @selector(collect:), srcMethodImplementation, method_getTypeEncoding(srcMethod));

  - (NSArray *) map: (TraversalBlock) block {
     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[self count]];
     for(id obj in self)
         [array addObject:block(obj)];
     return [array autorelease];

  - (NSArray *) uniq {
     return [[NSSet setWithArray:array] allObjects];


Come to think of it, I will take up this project myself - I will provide a link to the repository on Github when it is up

share|improve this answer
That is how I do it now, but I hoped for already existing project. Though your uniq method will not persist array elements order. –  tig Jan 10 '11 at 20:25
@tig Fixed the uniq method. –  Jacob Relkin Jan 10 '11 at 20:28
I never understood why this stuff wasn't added to Objective-C 25 years ago. I mean, how can you rip off Smalltalk but completely overlook its crown jewel, the collections library? How can you rip off Smalltalk without also ripping off inject:into, collect:, reject:, select:, detect:? –  Jörg W Mittag Jan 11 '11 at 3:02
@Jorg I know, right? That's why I'll be doing it! –  Jacob Relkin Jan 11 '11 at 15:53
@Allyn: I'd been meaning to create a similar library for a while, but this post motivated me to finally do it, so I started work on a library that adds methods from Smalltalk's collections protocol and Ruby's Enumerable mixin to NSArray. I'd love feedback and patches to the code. You can check it out here: github.com/mdippery/collections –  mipadi Jan 13 '11 at 18:46

I've ported Ruby's Enumerable API to Objective-C, and released it under the name EnumeratorKit.

Rather than being implemented in a category (or series of categories, duplicating the logic), it uses the Objective-C runtime to allow you to include the API into any collection class at runtime, and just like in Ruby all you need to implement is the -each: method. It also has EKEnumerator, which lets you do things such as enumeration over an infinite collection (e.g., the Fibonacci sequence).

share|improve this answer

Your Answer


