intro to rubymotion

a live coding adventure



Steve Tuckner

steve@coderow.com


Why Oh Why

xcode is great ;-)


  • Even though their code completion is very well done, it still feels too heavy for me
  • NIB's and now Storyboards are temptresses
  • I have to force kill it and restart it multiple times per day
  • IDE's are a language smell

RUby + iOS

a risky proposition


  • Even as objective-C gains ruby-style features: object literals, ARC, closures, ruby is still more concise
  • Ruby allows DSL's that can make programming a joy
  • The ability to do Ruby on the server and client is very attractive
  • Gem's don't generally port over to RubyMotion
  • No support for eval in its various forms
  • Sharing code between server and client can only be done in a limited way

RubyMotion

some caveats


  • It is only 6 months old, so its very young
  • The libraries that we take for granted in the rails community are not there yet
  • The implementation can be buggy still
  • There is no full screen debugger (just gdb -- just added)
  • Integration with 3rd party libraries can be a challenge (checkout the google-group for war stories)

WTF or FTW

because we love ruby


  • It allows me to work in Vim
  • nil.message is an error
  • testing is part of the culture and is supported nicely in the RubyMotion toolchain (Bacon functional testing is built in)
  • For this presentation I worked on three different libraries and did my changes all in a test first fashion.

iOS basics


  • Apps are written in Objective-C which is a cross between C and Smalltalk.
  • Large frameworks assist the building of apps
  • MVC based
  • Standard View to View navigation is done via pushing individual views onto a stack (like a browser) or by inserting views into tabs.
  • All assets (images, sounds, video, static data), are put together into a bundle.

iOS classes


  • UIApplication/UIApplicationDelegate
  • UIViewController
  • UIView - and all subclasses of that: UIButton, UILabel, UIImageView, etc.
  • Base Objects - NSString, NSArray, NSDictionary
  • C Structs - CGRect, CGSize, CGPoint
  • C Primitives - NSInteger, CGFloat, BOOL

App Structure


  • ApplicationDelegate - where execution starts
  • appDelegate.rootViewController - the container view controller that leads to all other view controllers and views
  • The root view controller is typically a UINavigationController, UITabViewController or a custom container view controller
  • UIViewControllers are pushed onto the container view controller to enable program flow

RubyMotion Intro


  • static ruby compiler using MacRuby implementation
  • build is done using Rake
  • standard project directory structure
  • plist settings are done via settings inside of the Rakefile

Objective-C --> Ruby


UIView *view = [[UIView alloc] init]

becomes

view = UIView.alloc.init

In RubyMotion/MacRuby, allocating objects that are Objective-C objects use .alloc.init instead of .new

OBJECTiVE-C --> Ruby #2


UIViewController *viewController = [[UIViewController alloc] initWithNibName: @"my-nib" bundle: nil]

becomes

viewController = UIViewController.alloc.initWithNibName("my-nib", bundle: nil)

OBJECTIVe-C to Ruby

MacRuby/RubyMotion unify primitives and objects so that you don't have to think about which is which unless you are passing pointers to framework classes.

NSString becomes String
CGRectMake becomes CGRect.new
NSInteger becomes Integer
CGFloat becomes Float
(^block) becomes Lambda

Task-it

the project



To write a simple app that has a Ruby on Rails server and a RubyMotion client.

Because what the world needs is another.....

todo list application

The model


class Task
   belongs_to :user
end

class User
   has_many :tasks
end

The Gems


  • BubbleWrap - wrap up a kitchen sink of functionality in to ruby friendly idioms
  • Teacup - a UI library for styling views like CSS and making that styling DRY
  • MotionModel - an in memory database (that can be serialized to disk) that looks and acts somewhat like ActiveRecord
  • Formotion - A UITableView form library

LIVE Coding

(sort of)

STEP 1

create a rails server 
with users and tasks scaffolding

Finish Server


STEP 2

  • add to layout to have links to users/tasks
  • display user name's instead of id
  • select user by name instead of id
  • test JSON interface with CURL

Create RubyMotion PROject


STEP 1

  • motion create step1
  • rake
  • (blank screen) - yeah!

Add in Views


STEP 2

  • Use a UINavigationController as the root view controller
  • Create a tasks controller to display the tasks
  • Use Teacup in setting up the view
  • Create a task controller to display/edit task details
  • Test it out

Add In Models


STEP 3

  • Create the model files
  • Create some model objects
  • Display the list of tasks
  • Use formotion to display/edit task details
  • Use REPL to play with the models

Get and Put Server Data


STEP 4

  • Use MotionModel to put JSON in and out of models
  • Use BubbleWrap to do HTTP calls

Add In FUnctional Testing

RubyMotion comes with RSpec style testing

  • Regular  model testing
  • Functional Testing using MacRuby Bacon library
  • Webstub ruby library that wraps NSURLProtocol to stub out HTTP requests

SUMMARY


I am @boberetezeke on Twitter and GitHub


Please come next month for our Ruby on Android talk!

Thanks for coming!

intro to rubymotion

By boberetezeke

intro to rubymotion

  • 1,119