Ruby string manipulation methods for NSString, implemented as a category.
- CocoaPods by adding
pod 'RubyCocoaString'
to yourPodfile
, runpod install
, and import the headers with#import <RubyCocoaString/NSString+RubyCocoaString.h>
. - Copy
NSString+RubyCocoaString.{h,m}
into your project, import it with#import "NSString+RubyCocoaString.h"
as necessary.
NSString
manipulation, while more fully-featured than you might imagine, is very verbose and burdensome when
compared to modern scripting languages such as Ruby and Python.
RubyCocoaString brings the string manipulation methods from Ruby
to NSString as a category. All semantics of the Ruby implementation of these methdods are preserved wherever possible,
contributions and issues welcome when inconsistencies are found.
Small Cocoa-isms are applied as necessary, however. For example, the Ruby code:
"".empty?
"foo".end_with? "o"
would be written as the following in Objective-C:
[@"" isEmpty];
[@"foo" endsWith:@"o"];
A few methods are also borrowed from ActiveSupport
, such as blank?
and present?
.
The following Ruby string methods are currently implemented:
capitalize
center
concat
downcase
end_with?
empty?
gsub
include?
lstrip
ord
prepend
reverse
rstrip
split
start_with?
strip
upcase
lowerCamelize
- Same ascamelize
, except first letter is lowercased. Helpful when, for example, an API returnsfoo_bar
and you want to assign it to an Obj-C object'sfooBar
property.
(Tasteful variations on Ruby string methods, specifically helpful on iOS/Mac, are allowed as Custom Methods, but this is not meant to be a library with every possible string method under the sun. Pull requests welcome!)
[@" foo bar " strip];
// => @"foo bar"
["foo_bar" camelize];
// => @"FooBar"
["foo_bar" lowerCamelize];
// => "fooBar"
[@"FooBar" underscore];
// => "foo_bar"
[" \t\n" isBlank];
// => YES
[@"foo bar" endsWith:@"bar"];
// => YES
[@"this is just madness" gsub:@"just" withString:@"pure"];
// => @"this is pure madness"
[@"this is JUST PLAIN madness" gsub:@"[A-Z]+"
withBlock:^(NSString *str, NSRange range) {
return ([str isEqualToString:@"JUST"] ? @"pure" : @"awesome");
}];
// => @"this is pure awesome madness"
[@"foo" eachChar:^(NSString *ch) {
NSLog(@"%@", ch);
}];
// => f
// => o
// => o
[@"telescope" reverse];
// => @"epocselet"
gsub
has some advanced semantic use cases which are difficult to satisfy, though most common use cases are semantically equivalent to its Ruby counterpart. It is used internally to implement many other RubyCocoaString methods. Part of the difficulty lies in semantic and syntactic differences in regular expressions between Ruby and Objective-C, and what is considered a 'match.'
Always write test cases before implementing methods. It's extremely easy, please ask if you've never written tests in Xcode before.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Write your tests, implement methods
- Change README and other applicable documentation reflecting your changes
- Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
A few other notes:
- Please try to keep things alphabetized as best as possible, both file orderings of test cases within Xcode's file navigator, and methods within the header/implementation files of
NSString+RubyCocoaString.{h,m}
. - This is an ideal library for coders new to the open source community to get involved, please do not hesitate to contribute or ask questions via issues, especially if you are new to testing in Xcode. I'm a nice guy :-)