The website for RubyMotion calls it “A revolutionary toolchain for iOS. It lets you quickly develop and test native iOS applications…using the awesome Ruby language you know and love”.
As a long-time Ruby (and Rails) developer, when I first heard about this I thought I was in a dream!
I have gone through those “…in 24 hours” books on iOS development. I have struggled with Objective-C (to the point that I dubbed it ‘Objectionable-C’). And I have fought with XCode’s peculiar “drag to connect an element with its event-handler” methodology.
The thought of writing pure Ruby that can be compiled into native iOS was thrilling. My imagination had me converting some of my Rails apps directly to iPhone apps with just a click!
RubyMotion does, in fact, compile to native iOS machine code. The apps you write with it are indistinguishable from Objective-C versions when submitted to Apple’s App Store. The code compiles down to LLVM machine code.
But, it is not quite as simple as writing MRI Ruby and compiling it to run on the iPad/iPhone/iPod.
Not quite. You still need to adhere to some of the structure and syntax of Objective-C.
However, the trade-off is more than fair. We get to apply our favorite Ruby idioms to iOS:
- Familiar dot notation
- Implied returns
- Linking of static libraries (iOS or other RubyMotion)
- Testing (based on MacBacon)
- Memory Management
- Interactive Debugger (just appeared in Version 1.24)
- Read-Evaluate-Print Loop (This single feature can ‘hook’ you on RubyMotion; in much the same way the DHH “Create a Blog in 15 minutes” brought a huge number of people to Rails)
Another key feature is the ability to freely mix Ruby and Obj-C in your code.
You can even write code, in RubyMotion, that can be statically linked into an XCode project!
In a recent talk for our local Ruby Users Group, I used this simple, albeit highly contrived, example to illustrate the contrast between Objective-C and RubyMotion
== OBJECTIVE-C ==
// Header (.h)
@interface Navigate: NSObjectc
@property (strong) NSString *name;
@property (strong) NSString *address;
@property (strong) CCLocation *location;
- (CLLocation)distanceFromLocation(CLLocation *)otherLocation;
// Messages (.m)
@synthesize name, address, location;
- (CLLocationDistance)distanceFromLocation(CLLocation *)otherLocation
return [self.location distanceFromLocation:otherLocation];
== RUBYMOTION ==
attr_accessor :name, :address, :location
The workflow in RubyMotion is **extremely** familiar to any Rails developer:
Using a specialized set of
Rake tasks you can compile your code – which links directly with the iOS SDK – and run it either on the iOS Simulator or directly on a connected device.
The Ruby Community to the Rescue
As stated earlier, this is not quite the Nirvana of writing Ruby code (that works in a Rails application, for example) and having it magically compiled to run on an iPhone. Although the iOS API is based on MVC and RubyMotion supports the concept of Controllers and Views, a working knowledge of the structure of iOS is still required when writing RubyMotion code.
Members of the active community continue to innovate and generate useful tools to aid in the progress of RubyMotion.
From the very beginning, the developer community began hacking and building, applying Ruby’s innate aptitude for Domain Specific Languages (DSL), and produced a set of utilities that “wrap” the cumbersome NeXTStep form with a more Ruby-esque syntax. The very first was Bubble-Wrap (appropriately named). Today you can find numerous tools to:
- Programmatically construct UI elements
- Build forms using familiar Ruby (hash) syntax
- Utilize XCode Interface Builder and Storyboard UI formats
And new iOS Apps are appearing on the Apple App Store, written in RubyMotion, almost every day. Some of the more recent include:
The build system is available as Open-Source on Github
Although RubyMotion is not free, all of these libraries and support tools have been created by the community and are available on Github. The development of RubyMotion has progressed at an impressive rate. HipByte, the company formed by Laurent Sansonetti – the creator of RubyMotion, is extremely responsive to the community with respect to fixes and updates.
My attempts to write iPhone/iPad/iPod/iAnything apps in ‘Obstinate-C’ had been a nightmare.
The ability to use flexible and forgiving Ruby and its expressive, English-like style make iOS development a dream now.
Resources to learn more
The RubyMotion website has an Introduction Video. There is also a Developer Center and FAQ on the site.
The Pragmatic Studio has a terrific Free Screencast that shows many of the highlights of RubyMotion.
For some deeper technical details, you can refer to the Runtime Guide on the RubyMotion website.
And, MotionCasts.TV is a set of community-generated screencasts specifically about RubyMotion.