Make beautiful apps with beautiful code: use the elegant and concise Ruby programming language with RubyMotion to write truly native iOS apps with less code while having more fun. You’ll learn the essentials of creating great apps, and by the end of this book, you’ll have built a fully functional API-driven app. Whether you’re a newcomer looking for an alternative to Objective-C or a hardened Rails veteran, RubyMotion allows you to create gorgeous apps with no compromise in performance or developer happiness.
July 22, 2014: All projects have been updated for compatibility with iOS7 and RubyMotion 2.0. Refined code to use more idiomatic Ruby when possible. Fixed external URLs that have been moved or deprecated. Fixed errata.
What You Need:
A Macintosh running OS X 10.7 or later is required to install RubyMotion. RubyMotion is a commercial product and currently requires a purchased license. Experience with the Ruby language and Ruby tools like RubyGems and Rake are suggested.
About this Book
- 112 pages
- Release: P3.0 (2014-07-29)
- ISBN: 978-1-93778-528-4
Developers interested in native iOS app development have been restricted to the limits of Objective-C and Xcode—until RubyMotion came along. RubyMotion enables you do full iOS development using Ruby. You have access to familiar tools such as Rake and RubyGems and can compile Ruby code into native applications. RubyMotion even comes with a Ruby console useful for live coding and interactive debugging. And since RubyMotion is built on top of the Objective-C runtime, you can use all of Apple’s libraries and most third-party code alongside Ruby classes.
This book takes you from zero knowledge of iOS development to building an app that displays information about colors using a web service API. You’ll work through each topic with a small example app that covers just enough to get you familiar with the techniques you need to get real work done.
You’ll start with just drawing boxes on the screen, but you’ll quickly add animations, a navigation stack, and high-performance table views. You’ll peek into less visibile components, such as using Ruby metaprogramming to create JSON-based models. You’ll use some incredibly useful common Ruby techniques that are only possible in RubyMotion, such as writing automated unit tests with an RSpec-like framework.
Using both Apple’s existing libraries and fresh, community-driven RubyMotion projects, you’ll be well on your way to writing real-world apps.
What You Need:
A Macintosh running OS X 10.7 or later is required to install RubyMotion. RubyMotion is also a commercial product and currently requires a purchased license. Experience with the Ruby language and Ruby tools like RubyGems and Rake are suggested.
To learn more about RubyMotion, have a look at this free screencast from Pragmatic Studio
Top 5 Tips for Working With RubyMotion
1. Use CocoaPods and don’t reinvent the wheel. CocoaPods allows you to easily import Objective-C libraries into your RubyMotion apps and use them just like any other iOS API, sort of like RubyGems. The iOS SDK has been around awhile longer than RubyMotion, and there are some very mature projects (like Nimbus and AFNetworking) that you can and should leverage whenever possible. For more info, check out the RubyMotion article on integrating with CocoaPods
2. Debug apps on your device. Very recently, RubyMotion added a debugging tool you can use while your app is running on your device. This allows you to set breakpoints, inspect variables, and get the backtrace of some tricky crashes. It’s not a full REPL like you can use on the simulator, but it really can help track down some particularly tricky problems. The RubyMotion developer docs are a great starting point: RubyMotion
3. Write your own RubyGems! In the book we use third-party libraries like BubbleWrap, but they don’t cover every facet of iOS development (yet!). If you find yourself using an iOS-C API and thinking, “Hmm, this isn’t very Ruby-like”, then absolutely consider writing a small wrapper and sharing it with the world. You need only to move a few files around and add a `Gemspec` file. You can convert your project to a RubyMotion-compatible RubyGem in a few minutes by following this guide: Gems
4. Use TestFlight to distribute beta versions of your app. TestFlight is a service that allows you to send in-development copies of your app to users without hooking up their device to your computer, as well as collect crash reports and usage statistics if you desire. The RubyMotion team has come out with a small gem which allows you to upload your app to TestFlight with one simple command, and it’s a real time-saver. It only takes a minute, so check out their post at TestFlight
5. Tell the community what you’re up to! The RubyMotion usergroup is very active and is continually collaborating on new libraries, tutorials, or just news announcements. So if you get your RubyMotion app on the App Store or publish some code on Github, definitely add a post. I hang around there too, and I’d love to hear if this book helped!
Q&A with Clay Allsopp, author of RubyMotion
Q: Why did you write RubyMotion?
There are so many developers who want to write iOS apps but are stymied at the idea of learning Objective-C, a very different language from Ruby. With RubyMotion, learning the nuances of Objective-C is no longer a requirement for developing for the iOS platform! I wanted to write a guide to help these developers become familiar with the iOS SDK and quickly get started on the right foot.
Q: How much Ruby knowledge do I need for the book?
You need some Ruby experience, as the book jumps right into the iOS SDK without delay. But don’t worry! Only familiarity with how standard object-oriented development (classes, inheritance, etc) works in Ruby is necessary. We do use some Ruby tricks here and there, like ‘instance_variable_get’, but they are explained and easy to pick up.
Q: What is your favorite RubyMotion feature?
The workflow is much more lightweight than the usual Xcode-centric development process. All of the tools are command-line based, from building your apps, to sending it over TestFlight, to running automated tests. I can work with the tools familiar to me (Rake, RubyGems, continuous integration) and end up with exactly the same result without compromising app quality.
Q: Would RubyMotion be helpful if I already know Objective-C?
Yes, because the RubyMotion community is working to push the core ideas of iOS and mobile development in new directions. The Ruby community has always had a propensity for crafting DSLs and new abstractions, and those efforts have carried over to RubyMotion. Projects like ProMotion and Formotion have no parallels in Objective-C iOS development, and they could be a real boon to your next project.
Q: Do you discuss third-party RubyMotion libraries in the book?
Yes, we use the popular BubbleWrap library several times and show how a few others (like TeaCup and Formotion) could speed up development. If you’re interested in more RubyMotion libraries, check out the list.
Q: Will I be able to build a working iOS app of my own after reading this book?
Yes! The last chapter builds a complete API-based app from scratch, which is an incredibly common task for many developers. But even before the end, you’ll be familiar with the most typical interface components and patterns and should be comfortable jumping into any new projects.
Contents and Extracts
- Ruby on iOS
- Hello, RubyMotion
- Reading This Book
- Online Resources
- Creating a New App
- Creating a New App
- Where It Begins: AppDelegate
- Showing a Message with UIAlertView
- Interactive Debugging
- Filling the Screen with Views excerpt
- All About Views and UIView
- Making Shapes and Colors
- Adding Interaction with UIButton
- Animating Views
- Adding Static Text with UILabel
- Making Text Dynamic with UITextField
- Exploring RubyMotion Libraries
- Organizing Apps with Controllers excerpt
- Adding a New UIViewController
- Using Multiple Controllers with UINavigationController
- Separating Controllers with UITabBarController
- Presenting Modal UIViewControllers
- Representing Data with Models excerpt
- Writing Basic Models
- Preparing Scalable Models
- Changing Models with Key-Value Observing
- Saving Data with NSUserDefaults and NSCoding
- Showing Data with Table Views
- Starting with Simple Rows
- Interacting with UITableViews
- Sections and Indexing Tables
- Swiping-to-Delete Table Rows
- Grouped-Style UITableViews
- Testing Your Apps
- Constructing Basic Tests
- Testing App UI and Controllers
- Example: Writing an API-Driven App
- Setting Up the Project
- Adding Hash-Friendly Models
- Making GET Requests and SearchController
- POST Requests and ColorController
- Uploading to the App Store
- Including App Resources
- Archiving for Release
- What’s Next?
Comments and Reviews
—Laurent Sansonetti lead developer of RubyMotion and founder of HipByte
Looking for the best way to get started with RubyMotion? Read this book.
—Ethan Sherbondy creator of Listenr for iPhone
RubyMotion was a joy to read, and after zipping through the book, I found myself full of motivation to write some code! RubyMotion is a great way for longtime Ruby programmers to quickly get up to speed on many practical aspects of writing native iOS applications.
—Dave Astels author of Test-Driven Development: A Practical Guide
The book is rich enough that, even though I have written many iOS apps in Objective-C and RubyMotion, with several in the App Store, I learned a few tricks. RubyMotion makes writing iOS apps faster, easier, and more enjoyable. This book gives you a jumpstart on that.