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

RubyMotion

Avatar for Caleb Caleb
September 26, 2013

 RubyMotion

Talk given to the Auckland Ruby Meetup on RubyMotion

Avatar for Caleb

Caleb

September 26, 2013
Tweet

Other Decks in Programming

Transcript

  1. What is it? It's a Ruby implementation that is staticly

    compiled and runs on the Objective-C Runtime RubyMotion is project created by Laurent Sansonetti, the original author of MacRuby Laurent was an Apple engineer, and left to release RubyMotion
  2. It's not free RubyMotion is a pay-for product. An annual

    cost of ~ $200USD gives you access to new versions as they are released MacRuby was open source, RubyMotion is not Although some parts of the toolchain have been released Apple Developer Programs - $99USD a pop p/a Community does release open source gems but there's a cultural difference
  3. Why? NSBitmapImageRep's initialiser: - (id)initWithBitmapDataPlanes:(unsigned char **)planes pixelsWide:(NSInteger)width pixelsHigh:(NSInteger)height bitsPerSample:(NSInteger)bps

    samplesPerPixel:(NSInteger)spp hasAlpha:(BOOL)alpha isPlanar:(BOOL)isPlanar colorSpaceName:(NSString *)colorSpaceName bitmapFormat:(NSBitmapFormat)bitmapFormat bytesPerRow:(NSInteger)rowBytes bitsPerPixel:(NSInteger)pixelBits;
  4. Native? Unlike HTML/CSS/JS framework alternatives: Frameworks like Phonegap, Appcellerator Titanium,

    Apache Cordova have downsides: 'Uncanny valley' effect UI controls trying too hard to look native but missing the mark Less performant Reimplement APIs themselves Have a lag time when new APIs are introduced However - have the benefit of a relatively smaller learning curve
  5. How is it different? It's a special kind of Ruby

    Named keyword arguments for Objective-C syntax Objective-C: [an_object function:with this:syntax] RubyMotion: an_object.function(with, this: syntax) (ala Ruby 2.0.0) Staticly compiled Ruby = Fast Ruby's object model on top of Cocoa Foundation classes and the Objective-C Runtime Can has all the APIs using Ruby syntax Managed memory - ARC-esque, and same performance App Store compliant
  6. Ruby classes implemented ontop of Cocoa classes (main)> Object.ancestors =>

    [NSObject, Kernel] (main)> "A string".class.ancestors => [String, NSMutableString, NSString, Comparable, NSObject, Kernel] (main)> {some: "hash"}.class.ancestors => [Hash, NSMutableDictionary, NSDictionary, Enumerable, NSObject, Kernel] (main)> 1.class.ancestors => [Fixnum, Precision, Integer, Precision, Numeric, Comparable, NSNumber, NSValue, NSObject, Kernel]
  7. Quick side trip Mutability? frozen_array = [:a, :b].freeze frozen_array <<

    :c => #<RuntimeError: can't modify frozen/immutable array> Cocoa base classes are immutable, and subclassed to be mutable i.e. NSMutableDictionary
  8. App Store compliance Slightly crippled Ruby no runtime code evaluation

    no require or eval put your requires in the Rakefile (or Bundler.require) for Rake to find and compile
  9. From experience... You still need to be able to read

    Objective-C to understand Apple's Docs. Cocoa development is as much understanding the frameworks and their APIs. Apple's documentation is the best place to do this.
  10. @interface Greeter : NSObject { } - (void)greet; @end @implementation

    Greeter - (void)greet { NSLog(@"Hello, World!"); } @end
  11. Ruby isn't just a language... Workflow Testable. Specs generated with

    each new project Console based workflow Use whatever editor you like rake build tasks REPL with magic powers (setting self) Preference of DSLs written in code over Interface Builder Pixate Formotion Teacup
  12. Ruby isn't just a language... Community Rubyists 'fixing' Cocoa APIs

    to be easier to use Projects like BubbleWrap, Formotion, Promotion basically, gems that end in 'motion' rubymotion-wrappers.com Ruby block syntax
  13. Ruby isn't just a language... Idioms Not there yet Objective-C

    camelCase versus snake_case Patterns that don't translate as well Design patterns built with language assumptions Implementing protocols Typing of objects Using complex types Different take on MVC to Rails 'ViewControllers' - presentational and interactivity Obj-C blocks are shorter and inline, whereas Ruby blocks can be long expressive configuration DSLs Verbosity
  14. BubbleWrap monkey patching the UIButton class to add a when

    method button.when(UIControlEventTouchUpInside) do self.view.backgroundColor = UIColor.redColor end
  15. data = { first_name: 'Matt', last_name: 'Aimonetti' } BW::HTTP.post("http://foo.com/", {

    payload: data } ) do |response| if response.ok? json = BW::JSON.parse(response.body.to_str) p json['id'] elsif response.status_code.to_s =~ /40\d/ App.alert("Login failed") else App.alert(response.error_message) end end
  16. Cocoapods It's like a bundler, for Cocoa projects. $ gem

    install cocoapods $ pod setup Using a Podfile, with a Gemfile-like DSL: platform :ios, '7.0' pod 'JSONKit', '~> 1.4' pod 'Reachability', '~> 3.0.0' Not exclusive to RubyMotion - Objective C Devs use Cocoapods for importing Objective C libraries
  17. RubyMotion generators A number of projects have similar boilerplate code

    to start with - such as UITableView or UITabBar apps Check out the RubyMotion Generators gem
  18. Resources RubyMotion Developer Guides RubyMotion Tips and Tricks Objective-C versus

    RubyMotion Objective-C to RubyMotion Convertor BubbleWrap Motioncasts Cocoapods Cocoa Controls