Slide 1

Slide 1 text

SWIFT FOR RUBYISTS

Slide 2

Slide 2 text

WHO AM I?

Slide 3

Slide 3 text

JP SIMARD @SIMJP REALM.IO

Slide 4

Slide 4 text

GITHUB.COM/REALM/REALM-COCOA

Slide 5

Slide 5 text

& OBJECTIVE-C

Slide 6

Slide 6 text

RUBY & OBJECTIVE-C COEXIST ▸ RubyMotion ▸ CocoaPods ▸ liftoff ▸ jazzy ▸ xcpretty ▸ lots more

Slide 7

Slide 7 text

RUBY & OBJECTIVE-C ARE SIMILAR ▸ common ancestor: smalltalk ▸ dynamic dispatch ▸ dynamic typing ▸ kind_of? ! isKindOfClass: ▸ respond_to? ! respondsToSelector:

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

&

Slide 10

Slide 10 text

FEW SIMILARITIES BETWEEN RUBY & SWIFT ▸ REPL ▸ Good for scripting: #!/usr/bin/xcrun swift ▸ Functional concepts in the standard library ▸ String interpolation

Slide 11

Slide 11 text

DIFFERENCES ▸ Swift is still a compiled language ▸ API's, Libraries & Frameworks ▸ Type safety & generics ▸ Swift doesn't work outside 

Slide 12

Slide 12 text

WHAT WOULD IT TAKE TO...

Slide 13

Slide 13 text

... RUN SWIFT OUTSIDE IOS/OSX? 1. Open source Swift compiler 2. Open source Swift runtime 3. Open source Swift standard library Objective-C is 30 years old and they still haven't done #3.

Slide 14

Slide 14 text

RUBY REPL == SWIFT REPL + PLAYGROUNDS

Slide 15

Slide 15 text

xcrun swift

Slide 16

Slide 16 text

Demo

Slide 17

Slide 17 text

1. Classes 2. Closures 3. Type Safety & Inference 4. Mutability 5. Functional Programming 6. Optionals 7. Generics

Slide 18

Slide 18 text

1. CLASSES

Slide 19

Slide 19 text

RUBY CLASS class Vehicle end class Car < Vehicle def initialize(model) @model = model end def drive "driving my " + @model end end car = Car.new('Batmobile') car.drive # => Driving my Batmobile

Slide 20

Slide 20 text

SWIFT CLASS class Vehicle {} class Car: Vehicle { var model = "" func drive() -> String { return "Driving my " + model } } let car = Car() car.model = "Batmobile" car.drive()

Slide 21

Slide 21 text

2. CLOSURES

Slide 22

Slide 22 text

RUBY CLOSURES def say_hello(&block) block.call end say_hello { puts "Hello there" } # => "Hello there"

Slide 23

Slide 23 text

SWIFT CLOSURES func sayHello(block: () -> ()) { block() } sayHello { println("Hello there") } // => "Hello there"

Slide 24

Slide 24 text

3. TYPE SAFETY & INFERENCE

Slide 25

Slide 25 text

RUBY'S DYNAMIC TYPE name = "John" name = Time.now() name = 123.45

Slide 26

Slide 26 text

SWIFT'S TYPE SAFETY & INFERENCE let anInt = 3 let aDouble = 0.1416 var pi = anInt + aDouble // Compile warning pi = 3 + 0.1416 // Compiles: number literals are untyped LIKE RUST & SCALA

Slide 27

Slide 27 text

4. MUTABILITY

Slide 28

Slide 28 text

MUTABILITY IN RUBY str = "abc".freeze # => "abc" hash = { str => { str => "value" } }.freeze # => {"abc"=>{"abc"=>"value"}} hash[str] = "foo" # => RuntimeError: can't modify frozen Hash hash[str][str] = "bar" # => "bar" hash # => {"abc"=>{"abc"=>"bar"}}

Slide 29

Slide 29 text

MUTABILITY IN RUBY let str = "abc" // => "abc" let hash = [str: [str: "value"]] // => ["abc": ["abc": "value"]] hash[str] = [str: "foo"] // => compile error hash[str]![str] = "bar" // => compile error

Slide 30

Slide 30 text

MUTABILITY IN SWIFT ▸ var is mutable ▸ let is immutable var letter = "a" letter = b // works let a = "a" a = "b" // compilation error

Slide 31

Slide 31 text

5. FUNCTIONAL PROGRAMMING

Slide 32

Slide 32 text

FUNCTIONAL PROGRAMMING IN RUBY numbers = [1, 2, 3, 4] numbers.map { |n| 3 * n } # => [3, 6, 9, 12] numbers.select { |n| n % 2 == 0 } # => [2, 4]

Slide 33

Slide 33 text

FUNCTIONAL PROGRAMMING IN SWIFT let numbers = [1, 2, 3, 4] numbers.map { (n: Int) -> Int in return 3 * n } // => [3, 6, 9, 12] numbers.filter {$0 % 2 == 0} // => [2, 4]

Slide 34

Slide 34 text

6. OPTIONALS

Slide 35

Slide 35 text

OPTIONALS var string = "" if string == nil {} // => compilation error: can never be nil var optString: String? if optString == nil { optString = "foobar" } if let forSureAString = optString { println("forSureAString: " + forSureAString) }

Slide 36

Slide 36 text

7. GENERICS

Slide 37

Slide 37 text

// Re-implement the Swift standard // library's optional type enum OptionalValue { case None case Some(T) } var maybeInt: OptionalValue = .None maybeInt = .Some(100) // Specialized Array var letters: [String] letters = ["a"]

Slide 38

Slide 38 text

LOTS MORE! ▸ Protocols ▸ Super-Enums ™ ▸ Structs ▸ Pattern Matching ▸ Objective-C interoperability ▸ Runtime

Slide 39

Slide 39 text

SWIFT != RUBY

Slide 40

Slide 40 text

FUTURE ▸ Swift will displace Ruby for Mac-only scripting ▸ Tools like RubyMotion likely won't be too affected

Slide 41

Slide 41 text

LINKS () ▸ Official Swift website (and blog) ▸ The Swift Programming Language Book ▸ WWDC Videos ▸ WWDC Sample Code ▸ Xcode 6 (and other resources) Free Apple Developer Account Required

Slide 42

Slide 42 text

LINKS (!) ▸ This talk: github.com/jpsim/talks ▸ From Ruby to Objective-C: speakerdeck.com/eddie ▸ Closures in Ruby ▸ Immutability in Ruby ▸ Why Rubyist Will Love Swift

Slide 43

Slide 43 text

THANK YOU!

Slide 44

Slide 44 text

Meetup().questions?.askThem!!

Slide 45

Slide 45 text

Meetup().questions?.askThem!! JP SIMARD, @SIMJP, REALM.IO