TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Parker Wightman.
Ruby string manipulation methods for NSString, implemented as a category.
pod 'RubyCocoaString'
to your Podfile
, run pod install
, and import the headers with #import <RubyCocoaString/NSString+RubyCocoaString.h>
.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 as camelize
, except first letter is lowercased. Helpful when, for example, an API returns foo_bar
and you want to assign it to an Obj-C object's fooBar
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.
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)A few other notes:
NSString+RubyCocoaString.{h,m}
.