Introduction to RubyMotion – The Cool Way to Build iOS Apps – @GantLaborde



Introduction to RubyMotion – The Cool Way to Build iOS Apps – @GantLaborde

3 4


RubyMotion-Intro

Presentation of Intro to RubyMotion

On Github GantMan / RubyMotion-Intro

Introduction to RubyMotion

The Cool Way to Build iOS Apps

By Gant Laborde

Scan the QR code for all slides AND references during this talk.

@GantLaborde

Avid Rubyist and generally immature guy

Co-Founder of IconoclastLabs.com

Author of Numerous RubyMotion projects and Pull Requests

Author of the book RubyMotion App Development by Packt Publishing.

Motion Meetup Organizer

RubyMotion's Monthly Meetup

meetup.RubyMotion.com

Matt Garrison

@mattsgarrison

RubyMotion

How many Ruby Devs? How many iPhone devs? How many game devs?

RubyMotion - Wat?

/ˈro͞obē-mōSHən/

noun trademark 1. A proprietary toolchain, created by HipByte SPRL, for developing native iOS and OS X applications on Apple devices and computers by statically compiling the Ruby programming language.

OR

Apps the Ruby Way

Ruby Rockstar!!!

Quick Example

Objective-C button tap

  [button addTarget:self action:@selector(buttonTapped:) 
      forControlEvents: UIControlEventTouchUpInside];

  //  Elsewhere
  - (void)buttonTapped:(id)sender {
      self.view.backgroundColor = [UIColor redColor];
  }
            

RubyMotion button tap

  button.addTarget(self, action:'button_tapped', 
      forControlEvents:UIControlEventTouchUpInside)

  # Elsewhere
  def button_tapped
    self.view.backgroundColor = UIColor.redColor
  end

            
I'm happy to announce I've never made anything in Obj-C. No that is not named parameters you see there.

Better Example

Objective-C button tap

  [button addTarget:self action:@selector(buttonTapped:) 
      forControlEvents: UIControlEventTouchUpInside];

  //  Elsewhere

  - (void)buttonTapped:(id)sender {
      self.view.backgroundColor = [UIColor redColor];
  }
            

RubyMotion button tap (with sugarcube gem)

  button.on(:touch) do
    self.view.backgroundColor = :red.uicolor
  end

            
Less delegates and targets; more Ruby blocks. Don't get me started on "addTarget:self".

lessLike:objectiveC:
and more like_ruby

- Clay Allsopp

What's So Great About RubyMotion?

In my opinion

  • Gems and Community
  • Editor Independence
  • Testing!
  • Readability
  • Natively Compiled
  • The REPL
  • Gems for RM as well as lots of existing gems! Some limitation, no eval, and statically compiled barriers. There are no dispassionate RM programmers.
  • Love Editor Independence, it's why I stay away from IDEs
  • BDD with MacBacon is easy!
  • Eye tracking study at Kent State for snake_case. No re-un-disenable
  • As opposed to other solutions (like PhoneGap) that do not
  • Read Evaluate Print Loop - similar to irb

RubyMotion Analogy

RubyMotion is to Objective-C

As Coffeescript is to Javascript

... plus gems are kind of like jQuery plugins

... plus MacBacon has spec tests like Jasmine would

... plus the REPL lets you live-modify like developer tools

... plus it compiles and obfuscates your final codebase

... OK, this analogy got out of control

Let's Look At Code!

  • Classic - Hello World
  • Full App - Ruby Trivia
  • Games!

Hello World

  $ motion create test 
        

Tests will fail

  class AppDelegate
    def application(application, didFinishLaunchingWithOptions:launchOptions)
      @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
      @window.rootViewController = HelloWorldController.new
      @window.makeKeyAndVisible
      true
    end
  end
          
  class HelloWorldController < UIViewController
    def viewDidLoad
      p "Hai"
    end
  end
          

Now tests pass!

This makes an iOS application, you can build an OS X application by passing in the correct template

If you are interested in a full TDD/BDD build of Hello World, check my book

Hello World - REPL

  main_view = UIApplication.sharedApplication.windows[0].rootViewController.view
  main_view.backgroundColor = UIColor.blueColor
        

OR, just command click the view!

  # REPL input
  self.backgroundColor = UIColor.whiteColor
  @label = UILabel.new
  @label.text = "Hello World"
  @label.sizeToFit
  self.addSubview(@label)
  
  # get down from there!
  @label.center = [100, 100]

          

OR, use sugarcube-repl's tree command.

Accessing the view via the UIKit framework is a pain! Fortunately, you can just command click

Can't command click because things are layered? Try sugarcube-repl's tree command.

  • a 2
  • a down 40
  • a.backgroundColor = :red.uicolor
  • a wider 100

.uicolor gets converted to objective-c. They all do! Take a look at `test = {}`, `test.class`, `test.class`, `test.class.ancestors`

Code Example #2

Ruby Trivia

  • sugarcube
  • Teacup
  • ProMotion
  • BubbleWrap
  • formotion
  • and more ...
BubbleWrap brings in little demi-god objects that help you avoid the need for intimate knowledge of Cocoa Touch dynamics but following them, whereas sugarcube gives you more ruby-esque ways of doing existing items and then some features that are completely new! Both maintianed by RubyMotion Group
iconoclastlabs.github.io/rubytrivia/
Available on itunes

Let's look at the code!

Code Example #3

Joybox

Joybox

joybox.io

  • Based on Cocos2D
  • Includes Box2D physics
  • Wrapped in Ruby API

EASY

Young, free, gaming framework! Cocos2D in Obj-C, and Box2D is in C++!

Joybox

Here, we see a screenshot, that Juan sent me on a game currently in production.

Let's look at some Joybox code!

Your browser can't show the video

Totally Tacos!

A game made in one day!

Thank you!

Questions?

@GantLaborde

Feel free to contact me via Gant@IconoclastLabs.com

Announcements
  • Motion Meetup
  • Rails Rumble
  • Rails Girls
  • books and sunglasses