$30 off During Our Annual Pro Sale. View Details »

RubyMotion

 RubyMotion

Native iOS Apps in Ruby - presented at SDRuby on July 5, 2012

James Miller

July 05, 2012
Tweet

More Decks by James Miller

Other Decks in Programming

Transcript

  1. RUBYMOTION
    Native iOS Apps in Ruby

    View Slide

  2. JAMES MILLER
    @bensie

    View Slide

  3. LAURENT SANSONETTI
    • Creator/maintainer of MacRuby
    • Left Apple, disappeared for awhile, re-emerged having created
    a startup that brings iOS development to Ruby
    • Plans to contribute parts of this work back to MacRuby

    View Slide

  4. PAIN POINTS
    When writing native iOS apps

    View Slide

  5. OBJECTIVE-C
    • Third-party libs still Objective-C, you need to know how to
    read it
    • RubyMotion syntax adds sanity but doesn't get rid of
    Objective-C
    textfromxcode.com

    View Slide

  6. XCODE
    • Forced into IDE
    • 11" air is terrible with Xcode
    • Tons of deep-down
    configuration, lots of dialogs
    • Buggy / Crashy
    textfromxcode.com

    View Slide

  7. RUBYMOTION
    What is it?

    View Slide

  8. WHAT IS IT?
    • Port of MacRuby for iOS
    • Ruby implementation on top of the Objective-C runtime and
    iOS foundation classes
    • Easiest Ruby version you'll ever install

    View Slide

  9. IS IT LIKE PHONEGAP?
    • No limits on APIs you can use because it's not wrapping APIs -
    the entire SDK is available and you implement Apple APIs
    directly
    • It does not bridge objects, it creates Objective-C objects
    directly
    • It does not run a bunch of code in a web view
    • It does not compromise on performance

    View Slide

  10. TRULY NATIVE

    View Slide

  11. TRULY NATIVE
    • Concurrent, no GIL, multi-core processors and GCD fully
    supported
    • Statically compiled to machine code (MacRuby uses JIT, which
    is not allowed on devices)
    • Full App Store compliance
    • Automatic memory management

    View Slide

  12. LIMITATIONS

    View Slide

  13. LIMITATIONS
    • require - cannot require files at runtime, they need to be
    available at build-time
    • eval - not supported in static compilation
    • define_method - not supported in static compilation
    • Proc#binding - removed because of performance issue
    • Regular Ruby gems do not work

    View Slide

  14. LIMITATIONS
    • No auto-complete - lots of copy-pasting Objective-C method
    names from docs and converting to Ruby
    • No stdlib
    • Debugger isn't awesome yet, but it's coming

    View Slide

  15. LANGUAGE BENEFITS

    View Slide

  16. NAMED PARAMETERS
    table = UITableView.alloc.initWithFrame(content_frame, style: UITableViewStylePlain)
    def tableView(tableView, didSelectRowAtIndexPath:indexPath)
    event = @events[indexPath.row]
    edvc = EventDetailViewController.alloc.init
    edvc.event = event
    edvc.title = event.name
    navigationController.pushViewController(edvc, animated: true)
    end
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    Event *event = [self.events objectAtIndex:indexPath.row];
    EventDetailViewController *edvc = [[EventDetailViewController alloc] init];
    edvc.event = event;
    edvc.title = event.name;
    [self.navigationController pushViewController:edvc animated:YES];
    }

    View Slide

  17. BASE64
    • Objective-C - find and
    download a Base64 library,
    add it as a dependency,
    figure out how to use it,
    encode your string...
    • Ruby
    ["string"].pack("m")

    View Slide

  18. HEADER FILES / PROPERTIES
    // Foo.h
    @interface Foo : NSObject
    {
    }
    @property (nonatomic, strong) NSString *bar;
    @end
    // Foo.m
    #import "Foo.h"
    @implementation Foo
    @synthesize bar = _bar;
    @end
    class Foo
    attr_accessor :bar
    end

    View Slide

  19. MIX AND MATCH
    RUBY/OBJ-C METHODS
    a = [1]
    a.objectAtIndex(0)
    # => 1
    b = NSArray.arrayWithObject(1)
    b.first
    # => 1

    View Slide

  20. MIX AND MATCH
    RUBY/OBJ-C METHODS
    String.ancestors
    # => [String, NSMutableString, NSString, Comparable, NSObject, Kernel]
    Hash.ancestors
    # => [Hash, NSMutableDictionary, NSDictionary, Enumerable, NSObject, Kernel]
    Array.ancestors
    # => [Array, NSMutableArray, NSArray, Enumerable, NSObject, Kernel]

    View Slide

  21. ENUMERABLE
    • #map
    • #each
    • #select
    • #include?
    • #sort_by
    • #group_by
    • #take
    • #......
    • #......

    View Slide

  22. BUILT-IN REPL
    Read eval print loop

    View Slide

  23. REPL
    • CMD + click on UI element from the simulator and make live
    changes to objects - selected object becomes self in console
    • Closest thing in XCode is setting a breakpoint

    View Slide

  24. RAKE-BASED COMMAND LINE

    View Slide

  25. RAKE / COMMAND LINE
    • Create projects
    • Test projects
    • Run project in simulator
    • Send app to device
    • Create IPA archive for App Store

    View Slide

  26. TESTING
    You can’t call it Ruby if you can’t properly test it

    View Slide

  27. TESTING
    • Ships with MacBacon (small RSpec clone)
    • Naive at this point, more robust functionality coming soon

    View Slide

  28. AND BY SOON I MEAN...

    View Slide

  29. TODAY!

    View Slide

  30. FUNCTIONAL VIEW AND
    CONTROLLER TESTING
    • Uses the functionality in Apple’s UIAutomation framework
    describe "The Timer view controller" do
    tests TimerController
    it "has a timer label" do
    view('Tap to start').should.not == nil
    end
    end

    View Slide

  31. DEPENDENCIES

    View Slide

  32. DEPENDENCIES
    • CocoaPods - Bundler-like dependency definition, automatically
    vendors and requires third-party libraries at build time
    app.pods do
    dependency "JSONKit"
    dependency "AFNetworking"
    dependency "libPusher"
    end

    View Slide

  33. BUBBLEWRAP
    The beginning of Ruby helpers for Cocoa APIs

    View Slide

  34. BUBBLEWRAP
    • Device
    • App
    • JSON
    • Notification Center
    • Observers
    • Location
    • HTTP
    • EM Reactor
    • Time
    • ..... fork and contribute!
    bubblewrap.io
    A collection of (tested) helpers and wrappers used to
    wrap CocoaTouch code and provide more Ruby like APIs.

    View Slide

  35. GETTING STARTED
    • Buy license for $199
    • $99 annual renewal
    • Renewal not required for continued functionality
    • iOS developer license from Apple ($99/year)

    View Slide

  36. THANKS!

    View Slide