Slide 1

Slide 1 text

iOS Development with Ruby using RubyMotion Francis Chong (@siuying) 12年7月26日星期四

Slide 2

Slide 2 text

Me • Francis Chong (@siuying) • Mobile and iOS app developer • Ignition Soft 12年7月26日星期四

Slide 3

Slide 3 text

` 12年7月26日星期四

Slide 4

Slide 4 text

Laurent Sansonetti (@lrz) 12年7月26日星期四

Slide 5

Slide 5 text

Released May 2012 12年7月26日星期四

Slide 6

Slide 6 text

What is RubyMotion? 12年7月26日星期四

Slide 7

Slide 7 text

Ruby on iOS 12年7月26日星期四

Slide 8

Slide 8 text

Statically compiled 12年7月26日星期四

Slide 9

Slide 9 text

Based on MacRuby (Ruby 1.9.x) 12年7月26日星期四

Slide 10

Slide 10 text

Custom GC 12年7月26日星期四

Slide 11

Slide 11 text

Core OS Core Service Media Cocoa Touch RubyMotion Objective-C 12年7月26日星期四

Slide 12

Slide 12 text

No extra layers! 12年7月26日星期四

Slide 13

Slide 13 text

Command line Toolchain 12年7月26日星期四

Slide 14

Slide 14 text

> motion create 12年7月26日星期四

Slide 15

Slide 15 text

> rake simulator 12年7月26日星期四

Slide 16

Slide 16 text

> rake device 12年7月26日星期四

Slide 17

Slide 17 text

RubyMotion Basics 12年7月26日星期四

Slide 18

Slide 18 text

Use Cocoa Touch classes 12年7月26日星期四

Slide 19

Slide 19 text

handle = [[NSFileHandle alloc] initWithFileDescriptor:2] [handle fileDescriptor] # => 2 handle = NSFileHandle.alloc.initWithFileDescriptor(2) handle.fileDescriptor # => 2 12年7月26日星期四

Slide 20

Slide 20 text

A Ruby dialect (based on MacRuby) 12年7月26日星期四

Slide 21

Slide 21 text

Named Parameters [string drawAtPoint:point withFont:font]; string.drawAtPoint(point, withFont:font) 12年7月26日星期四

Slide 22

Slide 22 text

Ruby objects implemented in ObjC • String > NSMutableString • Array > NSMutableArray • Hash > NSMutableDictionary • Numeric > NSNumber • Time > NSDate 12年7月26日星期四

Slide 23

Slide 23 text

Mixed ruby and Objective-C 12年7月26日星期四

Slide 24

Slide 24 text

'hello'.uppercaseString # => 'HELLO' 'hello'.upcase # => 'HELLO' 12年7月26日星期四

Slide 25

Slide 25 text

def iterate(ary) ary.each { |x| puts x } end iterate [42] iterate NSArray.arrayWithObject(42) 12年7月26日星期四

Slide 26

Slide 26 text

C Structures 12年7月26日星期四

Slide 27

Slide 27 text

struct CGPoint { CGFloat x; CGFloat y; }; typedef struct CGPoint CGPoint; CGPoint pt = CGPointMake(1, -1); 12年7月26日星期四

Slide 28

Slide 28 text

pt = CGPoint.new(100, 200) 'Hello'.drawAtPoint(pt, withFont: font) # Short Hand 'Hello'.drawAtPoint([100, 200], withFont: font) 12年7月26日星期四

Slide 29

Slide 29 text

Pointers 12年7月26日星期四

Slide 30

Slide 30 text

@interface NSData - (BOOL)writeToFile:(NSString *)path options: (NSDataWritingOptions)mask error:(NSError **)errorPtr; @end 12年7月26日星期四

Slide 31

Slide 31 text

# Create a new pointer to the object type. error_ptr = Pointer.new(:object) unless data.writeToFile(path, options: mask, error: error_ptr) # De-reference the pointer. error = error_ptr[0] # Now we can use the `error' object. $stderr.puts "Error when writing data: #{error}" end 12年7月26日星期四

Slide 32

Slide 32 text

Reuse existing Objective-C code base 12年7月26日星期四

Slide 33

Slide 33 text

or use CocoaPods 12年7月26日星期四

Slide 34

Slide 34 text

Why? 12年7月26日星期四

Slide 35

Slide 35 text

Easier to learn 12年7月26日星期四

Slide 36

Slide 36 text

Less boilerplates 12年7月26日星期四

Slide 37

Slide 37 text

Shorter, concise code 12年7月26日星期四

Slide 38

Slide 38 text

Build your own tool 12年7月26日星期四

Slide 39

Slide 39 text

Gems 12年7月26日星期四

Slide 40

Slide 40 text

BubbleWrap http://bubblewrap.io 12年7月26日星期四

Slide 41

Slide 41 text

HTTP BubbleWrap::HTTP.get("https:// api.github.com/users/mattetti") do |response| puts response.body.to_str end 12年7月26日星期四

Slide 42

Slide 42 text

KVO class ExampleViewController < UIViewController include BW::KVO def viewDidLoad observe(@text_field, :text) do |old_value, new_value| puts "Text field did changed: #{new_value}!" end end end 12年7月26日星期四

Slide 43

Slide 43 text

Location BW::Location.get do |r| puts "From Lat #{r[:from].latitude}, Long #{r[:from].longitude}" puts "To Lat #{r[:to].latitude}, Long #{r[:to].longitude}" end 12年7月26日星期四

Slide 44

Slide 44 text

SimpleView https://github.com/seanho/SimpleView 12年7月26日星期四

Slide 45

Slide 45 text

def viewDidLoad UI::Layouts.setup(view) do label width: 200, height: 20, text: "Choose your lucky word", color: "#eee" image_view top: 50, left: 50, right: 50, image: "sample.jpg" toolbar anchors: [:bottom] end end 12年7月26日星期四

Slide 46

Slide 46 text

Code Demo 12年7月26日星期四

Slide 47

Slide 47 text

12年7月26日星期四

Slide 48

Slide 48 text

Q & A 12年7月26日星期四

Slide 49

Slide 49 text

` 12年7月26日星期四

Slide 50

Slide 50 text

Q & A 12年7月26日星期四