I just started getting into Ruby Motion. At first I was somewhat frustrated. I felt that there was a lack of learning resources out there for a novice to iOS development, using RubyMotion. Reviewing Object-C (for me at least) was a turn off. I really wanted to pick up something easier and knowing Ruby I wanted to try out RubyMotion. The most recent book on RubyMotion (RubyMotion iOS development with Ruby) left me confused. It did simple things, but had less practical work.
Thankfully I came across a great resource called “Instant RubyMotion App Development.” This book would probably be too basic to someone familiar with Objective-C or RubyMotion. But for a guy like me who never wrote a app before – it’s perfect.
Creating The App
I use RubyMine (a Ruby based IDE from Jetbrains) to create my RubyMotion app’s. But you can do it from the command line with:
motion create --template=ios [app name]
If you use RubyMine, you click through the following screens:
This will create a project with a specific structure for us. Under the project title, we’ll have folders for App, Build, Resources and Spec.
App: This is where the application code goes
Build: I’m not sure what this is for yet! But it looks like it’s iOS version info.
Resources: Where you put your images/audio, etc. It is created with a default png file that is set to the iPhone resolution.
Spec: This is where your Ruby tests go. I’m not sure yet if Cucumber/Frank can be used with RubyMotion. But as I discover more, I’ll update this.
Previewing the App
If you run Rake (which is like ‘make’ in c) it will compile the project and run the iOS sim. For me it is defaulting to iPhone 6. But you can change that with some rake command line parameters.
So from the command line you can do:
within the project.
In RubyMine, you can simply click Run / Run ‘simulator: [project name]’
This will spawn a iOS simulator and will start the app.
Creating a Window
You can set an instance variable to define a window for the iOS screen. For starters we could put this code in the app/app_delegate.rb file (inside the def application(…) method. An example would be:
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
Of course @window is our instance variable. It is going to call the iOS class UIWindow, and initialize it using alloc.init
Alloc is a method to allocate memory for this action and initWithFrame is a way of setting the window/screen to the screen boundaries.
makeKeyAndVisible is an Objective-C method. It tells the compiler that this window is going to take the focus for user interaction. This is added simply as:
So the code might look like:
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.makeKeyAndVisible
Creating A Controller
Unlike Rails, we don’t have a nifty controller folder for our controllers. They go into the same app folder as all the rest of the app code. Let’s say we want to create a controller for some user input. If we create a file in the app folder called user_input_controller.rb and then add this content to it:
class UserInputController < UIViewController def viewDidLoad view.backgroundColor = UIColor.whiteColor end end
This class is inheriting from the UIViewController. The viewDidLoad method is a core method that runs to validate the loading of the view. In there we are simply saying “ok when it loads, make the background white.”
Back in the app_delegate.rb file we can modify it to have a pointer to the controller here, like so:
def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.rootViewController = MeditationTimerController.new @window.makeKeyAndVisible true end
Run the Sim
We can run the sim again with
It should load with the white background, which is code from our controller.