How you doin' RubyMotion

I'm doing a li'l dating with RubyMotion (And I really hope this relationship lasts ;) ).

By the end of this article (if you followed the steps, you would have written a very simple iOS app)

Steps:

Download RubyMotion from here

Rubymotion gives you some commands (similar to Rails), which you can check out by typing motion.

Of interest to us today is, the create command.

motion create 

It (just like rails new ) creates a new RubyMotion project.
This creates a set of files, along with some code. We'll go over them shortly.

Here's how the directory structure of your Rubymotion app would be like :
Directory structure of a rubymotion app

Now, take a peek into your RakeFile.

It tells ruby to add a specific path to the require search paths.
And then it configures the app. Right now the configuration you see is the app_name.

The file that was required above in the RakeFile brings with itself some tasks. (rake tasks)

You can check them out by doing :

rake --tasks

Running just rake, by default builds for the Simulator.

rake tasks

AppDelegate

Run:

rake config

to see the default configurations of your app.
You will come accross something called delegate_class.

What's this delegate_class?
Some setup work needs to be done before the app can be started. Once that's done, we need to give the system an object that can take over. It gets callbacks for application level events (like notification, when app is paused etc)

If you check the AppDelegate class, it just has one function
application:didFinishLaunchingWithOptions

You mostly will want to return true as the last line of this method.
You could use the options being passed into the method to determine whether you want to launch the app.

Now, just run rake on the command line, and see the simulator opening up. You can see Rubymotion is busy doing some processing, before it launches the simulator.

Now, what we are going to do is, add some code to show an alert on the app. An Alert with a title, a message and a button with some text on it.

def application(application,didFinishLaunchingWithOptions:launchOptions)
  @alert = UIAlertView.alloc.initWithTitle("Hey",
    message: "How you doin!?",
    delegate:nil,
    cancelButtonTitle: "Im doin Alrite. ",
    otherButtonTitles:nil
  )
  @alert.show
  true
end

Don't bother about what alloc is and how it works (for now)

You've built your first RubyMotion app. Go celebrate :)

Note - RubyMotion flavored Ruby

Ruby code in RubyMotion projects might look a little different from other Ruby projects. That's because RubyMotion flavored ruby has named parameters (which is now also a part of Ruby 2.0)

I'm still in the 'getting to know Rubymotion' phase, so if you've dated her in the past, and would like to share what you know about her, I'd really like to hear :)

Murtuza Kutub

Read more posts by this author.

Chennai, India

Subscribe to Murtuza's Blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!