Skip to content

michalkonturek/RubySugar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RubySugar

Twitter License CocoaPods Build Status

License

Source code of this project is available under the standard MIT license. Please see the license file.

Intro

Ports Ruby syntactic sugar to Objective-C.

id numbers = @[@0, @1, @2, @3, @4, @5];
id result = numbers[@"1..4"];
// [1, 2, 3, 4]

result = numbers[@"1...4"];
// 2, 3]

result = [@[@1, @2]:@[@3, @4]];
// [1, 2, 3, 4]

result = [@[@1, @2]:@3];
// [1, 2, 3]

[@3 rs_timesWithIndex:^(NSInteger index) {
    NSLog(@"Line #%i", index);
}];
// Line #0
// Line #1
// Line #2

result = @"Vexilla regis."[1];
// e

result = @"Vexilla regis."[@"1..6"];
// exilla

result = @"Vexilla regis."[@"1...6"];
// xill

result = [[[@"Number " :@10] :@" is greater than "] :@5];
// Number 10 is greater than 5.

result = [@"Hello" rs_chars];
// ["H", "e", "l", "l", "o"]

API

NSArray

- (instancetype):(id)object;

- (instancetype):(NSInteger)from :(NSInteger)to;
- (instancetype):(NSInteger)from :(NSInteger)to exclusive:(BOOL)exclusive;

- (instancetype)rs_clear;

- (instancetype)rs_combination:(NSInteger)n;

- (instancetype)rs_compact;

- (instancetype)rs_delete:(id)object;
- (instancetype)rs_deleteAt:(NSInteger)index;
- (id)rs_deleteIf:(BOOL(^)(id item))block;

- (instancetype)rs_drop:(NSInteger)count;
- (id)rs_dropWhile:(BOOL(^)(id item))block;

- (void)rs_each:(void (^)(id item))block;

- (id)rs_fetch:(NSUInteger)index;

- (instancetype)rs_fill:(id)object;
- (instancetype)rs_fill:(id)object withRange:(NSRange)range;

- (instancetype)rs_flatten;
- (instancetype)rs_flatten:(NSInteger)level;

- (BOOL)rs_includes:(id)object;

- (id)rs_inject:(id (^)(id accumulator, id item))block;
- (id)rs_inject:(id)initial withBlock:(id (^)(id accumulator, id item))block;

- (BOOL)rs_isEmpty;

- (NSString *)rs_join;
- (NSString *)rs_join:(NSString *)separator;

- (instancetype)rs_map:(id (^)(id item))block;

- (instancetype)rs_permutation;
- (instancetype)rs_permutation:(NSInteger)n;

- (instancetype)rs_reject:(BOOL (^)(id item))block;

- (instancetype)rs_reverse;

- (id)rs_sample;
- (instancetype)rs_sample:(NSUInteger)count;

- (instancetype)rs_select:(BOOL (^)(id item))block;

- (instancetype)rs_shuffle;

- (instancetype)rs_take:(NSInteger)count;
- (id)rs_takeWhile:(BOOL(^)(id item))block;

- (instancetype)rs_uniq;
- (instancetype)rs_uniq:(id(^)(id item))block;

- (instancetype)rs_zip;

- (id)objectForKeyedSubscript:(id<NSCopying>)key;

NSNumber

- (instancetype)rs_gcd:(NSInteger)other;

- (instancetype)rs_lcm:(NSInteger)other;

- (instancetype)rs_next;

- (instancetype)rs_pred;

- (id)rs_times:(void(^)(void))block;
- (id)rs_timesWithIndex:(void(^)(NSInteger index))block;

- (id)rs_downto:(NSInteger)limit do:(void(^)(NSInteger index))block;

- (id)rs_upto:(NSInteger)limit do:(void(^)(NSInteger index))block;

- (NSArray *)rs_numbersTo:(NSInteger)to;

NSString

- (NSString *):(id)object;

- (NSString *):(NSInteger)from :(NSInteger)to;
- (NSString *):(NSInteger)from :(NSInteger)to exclusive:(BOOL)exclusive;

- (NSArray *)rs_chars;

- (BOOL)rs_containsString:(NSString *)term;
- (BOOL)rs_containsString:(NSString *)term caseSensitive:(BOOL)caseSensitive;

- (NSString *)rs_delete:(id)input;

- (id)rs_eachChar:(void(^)(NSString *item))block;

- (BOOL)rs_isEmpty;

- (NSString *)rs_justifyLeft:(NSInteger)length;
- (NSString *)rs_justifyLeft:(NSInteger)length with:(NSString *)pad;

- (NSString *)rs_justifyRight:(NSInteger)length;
- (NSString *)rs_justifyRight:(NSInteger)length with:(NSString *)pad;

- (NSArray *)rs_split;
- (NSArray *)rs_split:(NSString *)pattern;

- (NSString *)rs_strip;

- (id)objectAtIndexedSubscript:(NSUInteger)index;
- (id)objectForKeyedSubscript:(id<NSCopying>)key;

Contributing

  1. Fork it.
  2. Create your feature branch (git checkout -b new-feature).
  3. Commit your changes (git commit -am 'Added new-feature').
  4. Push to the branch (git push origin new-feature).
  5. Create new Pull Request.