Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

VS OBJECTIVE-C RUBYMOTION

Slide 3

Slide 3 text

David Demaree Typekit UX & Ruby engineer at Adobe @ddemaree log.demaree.me [email protected]   ✉

Slide 4

Slide 4 text

Objective-C Invented in 1983, adopted by NeXT, currently maintained by Apple Pretty much used only for Apple platforms Statically typed object- oriented language Strict superset of C Preprocessed

Slide 5

Slide 5 text

RubyMotion Implementation of Ruby 1.9 (sort of) on LLVM and the Objective-C runtime Closed-source compiler, available commercially for $199 (+ annual support fee) Open-source build tools, based on Rubygems and Rake

Slide 6

Slide 6 text

WHY IS RUBYMOTION BETTER THAN ___?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

iOS Developer Agreement 3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).

Slide 10

Slide 10 text

iOS Developer Agreement 3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).

Slide 11

Slide 11 text

Ruby source code ObjC libraries Cocoa frameworks Native iOS app RubyMotion calls Cocoa frameworks directly

Slide 12

Slide 12 text

PhoneGap app (HTML/CSS/JS) Objective-C runtime Cocoa app (Objective-C) Cocoa app (RubyMotion) Compiles to Obj-C bytecode Compiles to Obj-C bytecode PhoneGap cross-platform runtime

Slide 13

Slide 13 text

Apple

Slide 14

Slide 14 text

5by5.tv/hypercritical/66

Slide 15

Slide 15 text

CODE

Slide 16

Slide 16 text

VS Objective-C Ruby syntax

Slide 17

Slide 17 text

CAUTION Now entering nerd zone

Slide 18

Slide 18 text

In the beginning, there was OBJECTIVE-C RUBY PYTHON JAVA SCALA GROOVY SMALLTALK

Slide 19

Slide 19 text

Learning Objective-C Programming with Objective-‐C bit.ly/objcprimer apeth.com/iOSBook cocoabook.com

Slide 20

Slide 20 text

Learning Ruby & RubyMotion All titles available at pragprog.com

Slide 21

Slide 21 text

Statically compiled No require No eval No Proc#binding No define_method Named parameters RubyMotion !≈ Ruby

Slide 22

Slide 22 text

Objective-C

Slide 23

Slide 23 text

Classic Obj-C Modern Obj-C Brackets everywhere Properties & dot syntax Verbose code for working with basic object types Object literals Manual memory management ARC No blocks Blocks!

Slide 24

Slide 24 text

Objective-C

Slide 25

Slide 25 text

Ruby can do almost anything Obj-C can do, BUT SIMPLER

Slide 26

Slide 26 text

@interface DDVenue : NSObject @property (strong) NSString *name; @property (strong) NSString *address; @property (strong) CLLocation *location; - (CLLocationDistance)distanceFromLocation:(CLLocation *)otherLocation; @end @implementation DDVenue @synthesize name, address, location; - (CLLocationDistance)distanceFromLocation:(CLLocation *)otherLocation { return [self.location distanceFromLocation:otherLocation]; } @end

Slide 27

Slide 27 text

class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end

Slide 28

Slide 28 text

class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object

Slide 29

Slide 29 text

class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing

Slide 30

Slide 30 text

class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing Implicit return

Slide 31

Slide 31 text

class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing Implicit return All dot syntax, all the time

Slide 32

Slide 32 text

class Venue attr_accessor :name, :address, :location def distanceFromLocation(location) self.location.distanceFromLocation(location) end end Everything inherits from (NS)Object Dynamic/“duck” typing Implicit return All dot syntax, all the time No interface file required

Slide 33

Slide 33 text

@interface DDFood : NSObject { BOOL isPizza; } - (void)setIsPizza:(BOOL)value; @end @implementation DDFood - (void)setIsPizza:(BOOL)value { isPizza = value; } @end class Food def isPizza=(pizzaness) @isPizza = pizzaness end def isPizza! @isPizza = true end def isPizza? @isPizza ||= false end end Instance vars must be declared Anything with an @-sigil is an ivar

Slide 34

Slide 34 text

// YellingString.rb class String def yell self + "!" end end "Kind of awesome".yell.yell.yell #=> "Kind of awesome!!!" // NSString+Yelling.h @interface NSString (Yelling) - (NSString*) yell; @end // NSString+Yelling.m @implementation NSString (Yelling) - (NSString*) yell { return [self stringByAppendingString:@"!"]; } @end [@"Kind of awesome" yell]; //=> @"Kind of awesome!" Classes extended via categories Classes can be reopened at any time

Slide 35

Slide 35 text

Ruby has real namespaces NSString, GBFont, DDCoreDataManager MyApp::Venue, MyApp::Item, CoreData::Manager Objective-C class names are prefixed Ruby classes/modules can be nested inside each other

Slide 36

Slide 36 text

Multiple inheritance via mixins module StaticTableViewController CellInfo = Struct.new(:text, :accessory_type, :action) def tableView(tableView, cellForRowAtIndexPath:indexPath) cell = self.infoForCells[indexPath.row] # Create and return a UITableViewCell end end module SettingsViewController < UITableViewController include StaticTableViewController end

Slide 37

Slide 37 text

class Thing < Struct.new(:name) def has_hat? false end end module Hat def has_hat? true end end

Slide 38

Slide 38 text

myThing = Thing.new("Cat") myThing.has_hat? #=> false myThing.extend(Hat) myThing.has_hat? #=> true

Slide 39

Slide 39 text

Blocks! class PlaceFinder def self.placeFinderWithBlock(&block) newFinder = self.alloc.init yield newFinder if block_given? return newFinder end end @finder = PlaceFinder.placeFinderWithBlock do |finder| finder.location = CLLocation.alloc.initWithLatitude(lat, longitude:lng) finder.numberOfResults = 10 end

Slide 40

Slide 40 text

HTTP.get("http://github.com/ddemaree") do |response| p response.body.to_str # prints the response's body end @singleton = nil Dispatch.once do @singleton ||= self.alloc.initWithOptions({}) end Providing a completion callback for a HTTP request Initializing a singleton in a thread-safe way

Slide 41

Slide 41 text

WHAT’S SO GREAT ABOUT SIMPLER?

Slide 42

Slide 42 text

Less boilerplate

Slide 43

Slide 43 text

Code is easier to read and more self-documenting

Slide 44

Slide 44 text

It… feels good?

Slide 45

Slide 45 text

What do we give up by using RubyMotion?

Slide 46

Slide 46 text

No compile-time warnings

Slide 47

Slide 47 text

No direct access to the C layer

Slide 48

Slide 48 text

Enums and C structs typedef NS_ENUM(NSUInteger, GBListSettingsSection){ GBListSettingsBudgetToggleSection, GBListSettingsSectionLock, GBListSettingsSectionEmail, GBListSettingsSectionAll }; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == GBListSettingsBudgetInclusionSection) { return 2; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return GBListSettingsSectionAll; }

Slide 49

Slide 49 text

class PlacesViewController < UITableViewController SECTIONS = [:budget, :lock, :email] def tableView(tableView, numberOfRowsInSection:section) sectionName = SECTIONS[section] if sectionName == :budget return 2 end end def numberOfSectionsInTableView(tableView) SECTIONS.length end end

Slide 50

Slide 50 text

The Uncanny Valley

Slide 51

Slide 51 text

> motion ri UIView RubyMotion `ri` documentation

Slide 52

Slide 52 text

RubyMotion’s “C” layer

Slide 53

Slide 53 text

RubyMotion’s C-like APIs Dispatch.once { @singleton ||= self.new } errorPtr = Pointer.new(:object) ABAddressBookCreateWithOptions(nil, errorPtr) Pointers are objects Functions are wrapped as methods on the Object class Grand Central Dispatch is wrapped as the Dispatch module

Slide 54

Slide 54 text

“Sexy” DSLs

Slide 55

Slide 55 text

Covering all of Cocoa with newer, simpler DSL abstractions is a stated goal of RubyMotion’s creators

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

alert = UIAlertView.alloc.initWithTitle "Hey, buddy", message: "Buzz off!", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: nil alert.show() App.alert("Hey, buddy", message: "Buzz off!") do |alert| # You can perform any additional configuration on the # UIAlertView object here, using the `alert` variable end BubbleWrap Standard Cocoa API (in RubyMotion)

Slide 58

Slide 58 text

github.com/alloy/MotionData

Slide 59

Slide 59 text

PhoneGap app (HTML/CSS/JS) Objective-C runtime Cocoa app (Objective-C) Cocoa app (RubyMotion) Compiles to Obj-C bytecode PhoneGap cross-platform runtime RubyMotion DSLs?

Slide 60

Slide 60 text

TOOLS

Slide 61

Slide 61 text

VS Bring your own Xcode

Slide 62

Slide 62 text

VS Familiar (if you’re coming from Ruby/open-source development) Lighter-weight, which can be faster / more nimble Have it your way Supported by Apple Designed specifically for Cocoa/Cocoa Touch app development Excellent integrated documentation & code completion

Slide 63

Slide 63 text

Xcode knows more about Cocoa & Objective-C than most of us will ever forget

Slide 64

Slide 64 text

Context-sensitive code completion

Slide 65

Slide 65 text

Integrated documentation

Slide 66

Slide 66 text

Real-time error checking (via static analysis)

Slide 67

Slide 67 text

“Using XCode is like driving a very used, modern car - it routinely breaks down, freezes and screws up the rest of my system and there is no way to understand what it wrong because all the parts are hidden from you. For example, it will stop compiling and simply freeze, acting like it's doing something. It will kill other processes and cause them to freeze (terminal processes simply stop getting cycles). It will stop responding to step over, step in, etc actions. “If there is any way for you to avoid XCode, do so. So far, I have had to force quit 4 times since 9:00 AM.”

Slide 68

Slide 68 text

better? Is

Slide 69

Slide 69 text

> motion create my_project

Slide 70

Slide 70 text

Motion::Project::App.setup do |app| # Use `rake config' to see complete project settings. app.name = 'GiftBox' app.version = "2.0" app.interface_orientations = [:portrait] app.identifier = "me.demaree.GiftBox2" app.short_version = "190" app.frameworks += ["CoreData"] end RubyMotion configuration DSL

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

github.com/mattt/shenzhen

Slide 75

Slide 75 text

TESTING

Slide 76

Slide 76 text

TESTING ON IOS

Slide 77

Slide 77 text

Client-side applications are big, messy balls of SHARED STATE

Slide 78

Slide 78 text

iOS client-side testing ≈ Full-stack JavaScript testing?

Slide 79

Slide 79 text

QUESTIONS

Slide 80

Slide 80 text

Should I learn Cocoa on Objective-C first, or RubyMotion?

Slide 81

Slide 81 text

Learn Objective-C Because it’s awesome (really)

Slide 82

Slide 82 text

Learning Objective-C Programming with Objective-‐C bit.ly/objcprimer apeth.com/iOSBook cocoabook.com

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

If you use RubyMotion, try to at least learn the Cocoa APIs

Slide 85

Slide 85 text

Should I use RubyMotion?

Slide 86

Slide 86 text

How I use RubyMotion today Personal projects Rapid prototyping Internal apps?

Slide 87

Slide 87 text

Your questions

Slide 88

Slide 88 text

David Demaree Typekit UX & Ruby engineer at Adobe @ddemaree log.demaree.me [email protected]   ✉