RubyMotion. Share Ruby love with your apps.

In this post and the following one I will talk about RubyMotion.

RubyMotion is a runtime for buiding native iOS apps using thethe Ruby language. Ruby, you say? Why Ruby? Well, Ruby is the only other language (the first obviously being Objective-C) officially supported by Apple for developing native apps.

While MacRuby, a Ruby interpreter on top of the Objective-C runtime providing a seamless bridge between Ruby and OS X Cocoa ecosystem, is an open-source project started in 2007 and backed by Apple itself, RubyMotion is a commercial project carried out by a team of engineers led by the very same individual, Laurent Sansonetti, that successfully launched MacRuby.

In the last few years some prominent Apple managers have gone as far as predicting that Ruby could be Apple's next language. Others are more skeptical about the subject, mainly because Objective-C is something Apple can directly control, and Ruby isn't. I tend to agree with the latter opinion, but it's still darn interesting to have an alternative. And it's even more interesting when we're talking about Ruby, a fast, fun, dynamic and prodcutive language. That's why RubyMotion, announced only in May 2012, is gaining momentum.

RubyMotion can leverage all the features provided by the Cocoa Touch APIs and at the same time harness the power and expressiveness of Ruby. To be more technical, by sharing the same object model infrastructure, Objective-C and RubyMotion APIs can be interchangeable at no additional performance expense. For example – in more concrete terms arrays inherits both from NSArray (Objective-C) and Enumerable (Ruby), allowing you to do something like:

@tableData = (1..10).to_a

def tableView(tableView, numberOfRowsInSection: section)  

def tableView(tableView, cellForRowAtIndexPath: indexPath)

  #setup the cell...

  cell.textLabel.text = @tableData[indexPath.row].to_s


...which to me seems pretty cool!

Another cool feature is that you have at your disposal kind of an interactive REPL: basically you simply build and run your app by running the rake command from the terminal, and while the Simulator is open, the Terminal should be displaying an irb-esque prompt where you can play in real time with you running app (you can for example change at runtime the attributes of your views).

For a live demo and more infos on RubyMotion you should definetely check out this PragProg's free screencast.

I've been playing with RubyMotion for the last couple of months and I like it. I really do. Anyway, I still feel more comfortable with Objective-C, although there are definetely some advantages that the Ruby language can count on (mainly when working with data - arrays, hashes and the like).

To say it all, in my opinion there are still a couple of drawbacks: code autocompletion and error detection. Both of these issues can be related to the IDE: Xcode. Let's run thru them one at a time:

  • error detection: the new Apple LLVM engine is constantly working in the background to understand the code and to alert the developer of coding mistakes. This is a feature that a dynamic language will probably never achieve. At the end of the day, Rubymotion is just plain Ruby code (with some little tweaks added to deal with keywords contained in Objective-C method syntax). You can use any text editor (TextMate, Sublime Text, put here your favorite text editor) to get the job done. Text editors won't give you error-detection.
  • autocompletion: no auto-completion in your text editor, means not having the means to tap easuly into Cocoa touch APIs. Glad for you if you can remember a method like - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath, but for mere mortals I think this is a no brainer feature to count on. Of course you can have snippets in TextMate or Sublime, but they can't stand a chance in comparison to what Xcode can do.

[EDIT] JetBrains has recently announced that the next version of RubyMine will support RubyMotion (with autocompletion features). You can already check out their early access preview by downloading it here. This seems promising and could speed up the development process of a RubyMotion app by an order of magnitude!

In the next episode I'll talk about what you can actually do with RubyMotion by providing a couple of concrete example.