GoGaRuCo 2012

RubyMotion: Rubyizing iOS Development

RubyMotion: Rubyizing iOS Development

by Amit Kumar

In this presentation titled 'RubyMotion: Rubyizing iOS Development' by Amit Kumar at GoGaRuCo 2012, the speaker discusses the innovative integration of Ruby into native iOS application development using RubyMotion. Emphasizing personal experiences and insights gained from using RubyMotion over several months, Kumar outlines the transformative aspects of this framework, detailing how it simplifies the development process for Ruby developers transitioning to iOS. Key points addressed include:

  • Introduction to RubyMotion: RubyMotion is defined as a tool allowing developers to write iOS applications using Ruby, streamlined for ease of access and enhanced functionality.
  • Runtime Integration: RubyMotion operates within the Objective-C runtime, facilitating direct interaction with native iOS classes, which aligns RubyMotion applications closely with traditional Objective-C development.
  • Automatic Memory Management: One of the highlights is RubyMotion's automatic memory allocation and reclamation, relieving developers from manual memory management tasks.
  • Interface Development: Developers can leverage Xcode's Interface Builder to design the application's user interface, with the XML files subsequently compiled within RubyMotion—this enhances productivity and efficiency.
  • Testing and Development Tools: Kumar elaborates on the built-in testing frameworks resembling RSpec, known as Matt Bacon, which aids developers in creating robust applications through seamless testing processes.
  • Community and Libraries: The RubyMotion ecosystem is enriched by support for popular gems such as Bubble Wrap and SugarCube, enhancing development capabilities by simplifying Objective-C code and providing layout tools.
  • Deployment and App Store Readiness: The process of creating, building, and preparing applications for App Store submission is discussed, highlighting project management through rake commands and configuration options.
  • Personal Experiences: Kumar shares anecdotes regarding his transition from Objective-C to RubyMotion, expressing satisfaction with the Ruby-based approach as opposed to conventional frameworks like PhoneGap, which face performance limitations due to their architecture.

In conclusion, Kumar's presentation offers invaluable insights into employing RubyMotion for iOS development, emphasizing that despite certain limitations (notably in debugging), its advantages, especially for Ruby enthusiasts, position it as a compelling choice for native iOS app development. As an incentive, Kumar offers a 15% discount for attendees interested in exploring RubyMotion further.

00:00:07.570 I think many of you have heard of RubyMotion. It's Ruby that runs on iOS, so you can finally use Ruby on your iPhone. Yay! Here to speak to us about that is Amit Kumar, he’s from Mumbai, India, but currently lives in New York.
00:00:20.950 Are we good to go, Michael? When I started making a technical adjustment here, I will do a little soft-shoe. I guess I’m supposed to do the Gangnam style, but that's enough of that. I’m not in five-inch heels, though, so I got it easier than Britney. Okay, are we good to go? Oh great, so here I am.
00:00:27.369 It has been great in San Francisco, coming all the way from Mumbai, India. Thank you, San Francisco and thank you, Josh. I'm here to talk about RubyMotion and Rubyizing iOS development. I have been doing RubyMotion for just a couple of months, and I would like to share my experiences and learnings about how I started getting into RubyMotion and what I have been doing so far. Before I do that, I’d like to quickly introduce myself. I have been doing Ruby development for almost four to five years and am a consultant at Tata Consultancy Services Limited.
00:01:12.010 This company manufactures cars, and that’s what I have been hearing from everyone whenever I say I work for Tata: 'Oh, the company that manufactures cars!' But Tata is a company that is involved in every business you can think of. That is my GitHub handle, Twitter, and my blog post. Now, I would like to do a quick survey: how many of you in the crowd are Objective-C programmers and have done it in Xcode? Keep your hands up if you love Objective-C!
00:01:50.140 Based on that, I can be safe in saying I'm not going to be killed here because about a year before I started learning Objective-C, I thought about learning it. This is what I ended up with: it was complicated! Everyone is talking about simplifying things, but I found it complicated. So I chose not to go that route. Just this May, something changed: Laurent Sansonetti, a one-man army, created RubyMotion. When it launched, I saw that and liked it so much that I went ahead and paid for it. It's a paid licensed product, and I have enjoyed it.
00:02:30.790 I have built three apps so far, one of which will be in the App Store pretty soon. Recently, Laurent was joined by Watson, who is a well-known developer from Japan. So what is RubyMotion and how does Laurent define it? Basically, Laurent says that it is a revolution for building native iOS applications using the Ruby language. I define it as a developer’s envy and a coder’s delight, which brings happiness.
00:03:05.310 In this presentation, we will see moments of happiness as a Ruby developer. Let's dive deep into what RubyMotion exactly is. RubyMotion is a fork of MacRuby, a Ruby implementation of Mac OS X that compiles into optimized machine code. In essence, the source code of Ruby is compiled into an optimized version of machine code which, in turn, gets converted into assembly language by LLVM. Laurent rewrote LLVM for RubyMotion because it was forked from MacRuby.
00:03:51.200 It has automatic memory allocation and reclamation, which is pretty cool. This is the first moment of happiness for me because I do not have to deal with memory management of objects; RubyMotion takes care of it. The other aspect is that it compiles the interface. If you have done a bit of Objective-C programming and have heard about storyboards and NIBs, you know you create interfaces in Xcode's Interface Builder.
00:04:35.479 With RubyMotion, you can create your interfaces in Xcode and import that XML file into your RubyMotion application. RubyMotion takes care of compiling it, which is neat. This is the second moment of happiness because it allows you to design the interface in Xcode and still import it into your RubyMotion application.
00:05:06.500 RubyMotion's runtime is tightly integrated with Objective-C's runtime, which means it has the same ancestors. All classes that you have in Objective-C can be used directly in your Ruby applications built with RubyMotion. For example, if you look at an NSArray, its ancestors are RubyMotion classes as well as the Objective-C classes like NSMutableArray.
00:05:39.960 So what this means is, you can potentially use shared objects between your Ruby code and your Objective-C code without any performance impact. When building a RubyMotion application, you are essentially building an Objective-C application because, at the end of it, it compiles into executables similar to those from an Objective-C application. There are differences, though, as RubyMotion is relatively new.
00:06:07.230 Let’s look at its architecture. As I said, RubyMotion and Objective-C share the same runtime with the same ancestors, which is the Foundation framework. You can potentially call all the SDK APIs from your Ruby application. When I started learning RubyMotion, there were a few questions I asked myself and that I have been asked by others. I call them myths.
00:06:44.520 The first question I was asked multiple times is: 'Can I code in RubyMotion without the pain of learning the Cocoa framework?' I would like each of you to answer that question. Do you think it’s possible? Exactly! My answer is no. Rather, I feel that if you are coding in RubyMotion, you are more closely tied to the Cocoa framework.
00:07:03.840 As Ruby developers, we want to control things. By control, I mean not controlling everyone, but controlling the code we write. We do not believe in magic; we want to build our interfaces ourselves through code. So let’s look at another powerful aspect of RubyMotion.
00:07:39.010 The second question I was asked is: 'What about frameworks like PhoneGap?' Are any of you fans of PhoneGap-like frameworks? Yes, there are these tools which do similar things, so why should I care about learning RubyMotion? My answer would be: let's look at the pros and cons of PhoneGap.
00:08:25.490 The first benefit is ease of use; you code in HTML, CSS, and JavaScript, which many of us love. JavaScript is seen as the next big thing. However, there are a few drawbacks. PhoneGap operates through a bridge, and whenever there is a bridge involved, you run into performance bottlenecks. Apple does not optimize UIWebView.
00:09:08.240 Additionally, it follows a single-thread model, resulting in about a quarter of the performance that you would get in a native application. You are also limited by the support of PhoneGap and debugging becomes extremely painful because it uses the native JS bridge Apple provides to make native API calls.
00:09:44.750 So, how do I download RubyMotion? As I mentioned, it's a licensed product, and you need to pay two hundred dollars. Stay tuned, as there’s a surprise at the end of the session, so keep your eyes open. After downloading it, you install one command that kicks things off, called the motion command, where you have four options.
00:10:02.139 The first is 'motion create,' which helps you create a vanilla application. The second is 'motion activate,' which activates the license. It's pretty straightforward. Then there’s 'motion update’ for software updates, and 'motion support,' which is cool because it gives you access to RubyMotion’s support ticket system from your console.
00:10:55.579 After creating your first application, your project structure will look something like this: you will have a rake file, along with the app delegate class, which serves as the point of delegation entry into the application. You also have resources and a spec directory, which we will talk about shortly. Now, let's address the motion rake command specifically.
00:11:57.530 Most Ruby developers know what rake is, and it's your bread and butter in RubyMotion for developing, testing, and deploying applications. The starting point is 'rake' command, which builds your application and runs it in the simulator. The second command is 'spec,' which runs tests against your application.
00:12:55.740 RubyMotion comes with a built-in testing framework that resembles RSpec, called Matt Bacon, which has all the flavor of RSpec syntax that you may be used to. The 'build' command archives your application and creates an IPA file, which you can distribute or push to the App Store. There are other commands as well, but as a new developer in RubyMotion, those functionalities should suffice for now.
00:13:54.300 One important command is the 'reconfig' command, as every iOS application has numerous configuration options. This command lists down all the configuration options available for your application. It contains valuable information such as the name of your application, the app delegate class, framework options, and device family specifications.
00:14:45.260 Moreover, RubyMotion compiles your Ruby source code alphabetically by default, which can lead to compilation issues when there is a class inheritance that does not follow that order. When you encounter such situations, it’s essential to instruct the compiler to redefine the compilation order.
00:15:04.780 Additionally, you have access to vendor projects that help you use external libraries in your RubyMotion applications. This is a powerful addition that enables seamless integration of external libraries, which can also be Objective-C libraries. Let’s take a look at the soul of RubyMotion.
00:15:43.490 The soul of RubyMotion, which I love so much, is the REPL (Read-Eval-Print Loop), which facilitated my understanding of the Cocoa UI model. I’m going to take a risk and do a quick demo to illustrate how it works.
00:16:12.230 For this demo, I created a default application. As you can see, I have integrated a bundler dependency management tool into it. For the purposes of this demonstration, I will simply launch the application.
00:16:41.700 This is where your control will begin. Let’s grab the instance variable of the controller from the console by utilizing some syntactic sugar from Bubble Wrap, which is a RubyMotion gem. Okay, so I’m going to run that now.
00:17:04.700 I got the instance handle of the controller. Let’s change the background color of the view. Which color do you prefer? Blue? Green?
00:17:43.400 Now I will add a control on the fly, specifically a segmented control. Once I correctly create this control and add it to the view, I should see it in action.
00:18:12.240 This is the Ruby syntax I’ve written so far, and I love it. Fingers crossed that this works! And I was successfully able to add a control from the command line, which is awesome.
00:18:46.130 Next, let's move it down by 200 pixels and 100 pixels to the right. I’m going to use an additional RubyMotion gem called SugarCube to accomplish that.
00:19:41.400 Here’s the view I want to adjust: I have the handle to that specific view now. Let’s move it down and towards the right.
00:20:22.570 While doing this, I noticed something interesting. If I hold the Command key, I can easily drag and move UI elements within the simulator. You see, it changes to an interactive view as I hover, which is quite intuitive!
00:20:53.790 I can change the label of the segmented control by navigating through the hierarchy of classes. This is an excellent feature of the RubyMotion framework.
00:21:30.230 I have successfully completed the demo and feel in control. This is yet another moment of happiness!
00:22:01.370 I encourage you to check out an in-browser demo of Ripple. You can visit a specific link to see three applications and learn about the build process, which consists of four main steps: compiling, linking, packaging, and an optional code signing step.
00:22:52.290 With RubyMotion, when you fire the rake command, all your Ruby code is compiled into machine code, which is converted into assembly language with LLVM. The machine code is linked with RubyMotion statically and then packaged or copied into your application’s build directory.
00:23:52.890 You’ll end up with a fully compiled code ready to be pushed to the App Store. You may even create an IPA file ready for distribution. When it comes to testing, RubyMotion comes pre-packaged with an RSpec-like framework called Matt Bacon.
00:24:52.390 This provides the basic structure for testing your application, including assertions, matchers, and before/after blocks. You’ll find a default test that passes simply to serve as a starting point for your application. Testing is crucial in ensuring that your application is functional and stable.
00:25:36.890 As we navigate through testing, let's return to the spec directory. Every application has a 'main_spec.rb' file, containing a simple assertion that checks if your UIWindow is created.
00:26:16.240 Let's run this test, and it should launch the simulator again as the test is executed in that environment.
00:26:48.690 Cool, it passed, showing one window and one specification, with zero failures!
00:26:55.220 Matt Bacon includes syntactic sugar similar to what you would find in the RSpec framework, along with facilities to test your views that are built using Interface Builder.
00:27:49.780 In order to test shared applications internally for a client, we set up a continuous integration system using Jenkins. Whenever code is checked in, it triggers a process that publishes the application internally using Apple’s over-the-air deployment technology.
00:28:37.930 In fact, RubyMotion provides a seamless way to manage external libraries through four methods: Ruby gems, Objective-C projects, native C code, and CocoaPods. This allows developers to integrate various libraries into their RubyMotion applications.
00:29:09.970 RubyMotion gems differ from normal Ruby gems due to its static compilation. You can use Bundler to manage dependencies effectively by creating a Gemfile and including it in your Rakefile.
00:29:56.900 The RubyMotion community has developed several popular gems: Bubble Wrap, which reduces verbosity in Objective-C code, SugarCube for visual layouts, and Teacup for stylesheet-like development.
00:30:41.540 All these libraries enhance the RubyMotion experience, making development enjoyable and efficient. In summary, RubyMotion has come a long way, but certain features such as debugging are still limited.
00:31:13.390 However, despite its youth, RubyMotion applications have already seen success in the App Store. Apps like Kappa Phi and others are leading the charge, with more on the way!
00:32:08.530 Thank you for your attention. As a token of appreciation, I can offer you a 15 percent discount if you’re interested in trying RubyMotion. Just shoot me an email, and you'll get this discount.
00:32:46.440 Thank you very much!