I blog about Ruby on Rails, coding, and servers

RubyMotion Tips & Tricks

Reading time 6 minutes

I’ve been using RubyMotion for four days now. I still like it a lot, and I think the coolest thing about it is the rapidly-evolving community around its use. Ian Phillips figured out how to link Interface Builder and RubyMotion; Alan deLevie wrote a neat connection to Parse.com. Personally, I’ve been spending the time writing lots of code. I’ve just deployed my first RubyMotion app into TestFlight and sent it to some testers. Soon hopefully I’ll have an actual RubyMotion app, which is pretty exciting! I’ve learned a lot in the last four days of use and wanted to post a few tips and tricks to hopefully make life easier for other RubyMotionists.

Wrap It Up

I’m using NSUserDefaults to store some cached data so I don’t flood my server. Calling NSUserDefaults constantly was really getting me down though, so I made this cute little wrapper class that does all the hard work for me.

I only want to cache the data for one hour (3600 seconds); that’s why I only return true for exists? if the keys are younger than that. Using this cache is really simple.

1234567

@data=ifCache.exists?('cache_key')Cache.read('cache_key')elseresults=1# Populate the cache with slow work hereCache.write('cache_key',results)resultsend

The only caveat to this wrapper – something I need to look at more – is that you can only store property values in it. In other words, no custom classes, which sort of sucks if you have a lot of models that you want to hold somewhere. I’m getting around this by storing IDs and then looking them up afterwards in my model. I could use CoreData instead (and the sample app has an excellent example of how to use it) but that’s a level of complexity I don’t think my app needs yet.

Don’t Be Afraid To Use Blocks

Blocks are one of the coolest parts of Ruby! A lot of Objective-C code can be made more readable and more beautiful by sticking it in block syntax. Here’s a little UIView helper to show you what I mean.

There’s a lot of stuff I ended up wrapping in blocks just for sanity’s sake; having to commitAnimations or synchronize after every operation is a big downer. Remembering to end blocks is quite a bit easier, and as a bonus makes your code look a lot more readable as well.

Make Shortcuts

I stole this one rather blatantly from Sam Soffes. It’s trivially easy to open up classes and add your own methods to them:

Then you have your own cool code like UIColor.darkBlueColor next to the standard UIColor methods. If you find yourself copying and pasting something over and over, don’t! It’s super easy to make extensible, quick shortcuts in Ruby. Take advantage of that.

Stand on the Shoulders of Giants

There are a lot of RubyMotion projects popping up out there, and there are a lot of existing Cocoapods projects. Why reinvent the wheel? Use other peoples’ smarts to propel your own project ahead quickly and easily; if you find yourself outgrowing your dependencies, then you should consider doing them yourself, but not before.

For example, I’ve been using AFNetworking to do my networking management, connections, and callbacks – they already made the whole NSURLConnection stuff easy to use, and I gotta say using it in Ruby is super easy too:

Of course, you’ll want to do something a bit more interactive with your error than just log it, but you get the idea. Meanwhile, on the RubyMotion side, I stole quite a bit of code from BubbleWrap, specifically the really excellent kernel shortcuts. Being able to do simulator? or ipad? is really awesome and a big time saver. I really hope that RubyMotion gets its own concept of Gems soon; it’ll make receiving code faster than just copying and pasting it directly into my source.

As I get closer to releasing the app I’m working on, I’m sure I’ll have more interesting tips and tricks to share: these thoughts are really all that stood out to me after a quick review of my code. As I said at the top, I’m still really impressed with RubyMotion; I’ve had a few more random crashes without error messages or backtraces, but that’s my only real complaint so far. We’ll see how RubyMotion fares as my app moves towards release, but I am personally hopeful, and at the very least it’s made iPhone programming a lot less painful.

Josh Symonds performs devops and server wrangling on cloud-scale infrastructures, deploys amazing web applications with Ruby on Rails, and creates awesome iOS apps with Objective-C and RubyMotion. He is founder and CTO of Symonds & Son, a development shop focused on quality and excellence.