Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. 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
  2. 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
  3. XCODE • Forced into IDE • 11" air is terrible

    with Xcode • Tons of deep-down configuration, lots of dialogs • Buggy / Crashy textfromxcode.com
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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]; }
  10. 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")
  11. 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
  12. MIX AND MATCH RUBY/OBJ-C METHODS a = [1] a.objectAtIndex(0) #

    => 1 b = NSArray.arrayWithObject(1) b.first # => 1
  13. 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]
  14. ENUMERABLE • #map • #each • #select • #include? •

    #sort_by • #group_by • #take • #...... • #......
  15. 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
  16. RAKE / COMMAND LINE • Create projects • Test projects

    • Run project in simulator • Send app to device • Create IPA archive for App Store
  17. TESTING • Ships with MacBacon (small RSpec clone) • Naive

    at this point, more robust functionality coming soon
  18. 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
  19. 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
  20. 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.
  21. GETTING STARTED • Buy license for $199 • $99 annual

    renewal • Renewal not required for continued functionality • iOS developer license from Apple ($99/year)