RubyMotion is a commercial toolchain for iOS development using the Ruby programming language. It implements Ruby on top of the Objective-C runtime and iOS Foundation classes. Created by former MacRuby developers, it is THE tool that Ruby community has been waiting for to create fast and native iOS applications. On the other hand, it has also drawn lots of attention from iOS community. Someone with just minimal iOS experience is able to write an application and get it running on devices thanks to Ruby’s expressiveness.
Our sample app in RubyMotion maintains the same functional flow and UI structure as our iOS sample app. Meanwhile we try to be more ruby-ish in our implementation. This article aims to show you some interesting points we encountered during the process of discovering this awesome tool – RubyMotion.
Usage of Cocoapods
As mentioned in our previous blog post, we start to use Cocoapods to manage our iOS SDK. You can now skip all the manual configuration steps and just use a Podfile to manage Moodstocks iOS SDK or other 3rd party libraries.
RubyMotion and preprocessor macros
We came across a compilation problem at the beginning of the implementation:
We traced the problem and then did a comparison between the .bridgesupport file generated WITH and WITHOUT our MS_SDK_REQUIREMENTS preprocessor macro – which is enabled if the target is the device (arch = armv7) and the base iOS SDK is at least iOS 4.0.
We can see that RubyMotion skips all the code between this macro:
In order to better understand the problem, here is a piece of source code extracted from RubyMotion’s opensource part.
We can see that gen_bridge_metadata always targets the Simulator platform, which leads to the arch flags being skipped during the build process. According to the creator of RubyMotion Laurent Sansonetti:
@deltheil @bonbonking it uses the sim platform because it generates and executes temporary C programs (so it has to target it).
— Laurent Sansonetti (@lrz) January 21, 2013
So some modifications have been carried out to cope with this issue.
Delegation in RubyMotion
“Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled.” [Source]
The implementation of delegation in RubyMotion is slightly different from the one in Objective-C code. Instead of defining the delegate protocol and methods in .h file then implement them in .m file. In RubyMotion you just have to implement the essential idea of delegation. Below is a delegation example in our sample app, originally inspired by a gist by @joffreyjaffeux. The delegating object in our example is “ActivityView”, its delegate is “ScannerViewController”. When a user taps on the scanning view, an activity view will appear above the scanner, and ActivityView will keep a reference to ScannerViewController. When a user clicks on the “Cancel” button of the activity view, the cancel action will tell ScannerViewController to cancel the scan session.
Other thoughts
RubyMotion is a revolutionary tool. It not only offers developers a new way to build iOS applications, but also shows great experimental spirit. If you are a Ruby developer, you will probably be very happy to get started with iOS development in your familiar language. Just pay attention to this point: some cool Ruby features may not be applicable in RubyMotion. As @clayallsopp said in his article about RubyMotion metaprogramming, “the string-based eval methods (which rely on an full-blown interpreter) and Bindings are not available in RubyMotion”.
If you are an iOS developer, we agree Objective-C is a great programming language, but you might find RubyMotion a very nice way to speed up the development process. Plus, you have a very active community and an excellent team behind this. As they said, you are definitely in good hands.
One of the very few downsides of RubyMotion is that it is a little bit long to compile since it requires an Internet connection.
Some resources
We thought these resources might be handy for your RubyMotion journey.
Tutorials: RubyMotion Tutorials & THE book to get started with RubyMotion
Tools: Sublime RubyMotion Auto-completion & RubyMine support RubyMotion in IDE
Also, if you are looking for a higher-level wrapper, make sure to check Motionscan (which is a work-in-progress project by @joffreyjaffeux).
Finally, go to our github page to check out our demo app!

Posted on 30,Jan |
Posted by Qian Jin 





There are no comments yet