Swift for Ruby & Javascript Developers

Presented by Jared White for

North Bay App Developers

meetup.com/North-Bay-App-Developers/

@jaredcwhite

Ruby

​i = 3


@name = "John Doe"


self.cat = Cat.new(:tabby)

puts self.cat

Javascript

var i = 3; // or let


this.name = "John Doe";


this.cat = new Cat('tabby');

console.log(this.cat);

Swift

let i = 3


self.name = "John Doe"


self.cat = Cat("tabby")

print(self.cat)

Ruby

a = 1   # assign an integer
b = "a string"   # assign a string
c = [2, 4, 6, 8]   # assign an array
d = {abc: "some", xyz: "values"}   # dictionary / hash

VARIABLES

Javascript

let a = 1;
let b = "a string";
let c = [2, 4, 6, 8];
let d = {"abc": "some", "xyz": "values"};

VARIABLES

note: let is the new, improved alternative to var in ES6, because reasons.

Swift

let a = 1
let b = "a string"
let c = [2, 4, 6, 8]
let d = ["abc": "some", "xyz": "values"]

VARIABLES

note: let here is NOT the same thing as let in ES6. let in Swift creates a constant, aka a "variable" that is actually immutable.

Ruby

a = 12345
b = "a string with #{a} in it"
puts b.include? "123"   # true

STRING INTERPOLATION

Javascript (ES6)

let a = 12345;
let b = `a string with ${a} in it`;
console.log(b.indexOf("123") != -1);  // true
console.log(b.includes("123"));    // ES6: true

STRING INTERPOLATION

note: that backticks syntax looks pretty eewww, but it's definitely useful

Swift

let a = 12345
let b = "a string with \(a) in it"
print(b.contains("123"))  // true

STRING INTERPOLATION

note: that backticks syntax looks pretty eewww, but it's definitely useful

Ruby

range1 = 1..5   # 1, 2, 3, 4, 5

 

range2 = 1...5  # 1, 2, 3, 4

RANGES

Javascript

RANGES

(just kidding, Javascript doesn't have ranges)

Swift

let range1 = 1..<5   # 1, 2, 3, 4

 

let range2 = 1...5  # 1, 2, 3, 4, 5

RANGES

note: OMG, ... is the opposite of Ruby!

Ruby

myArray = [1,7,8]
growingArray = []
myArray.each do |i|
  growingArray << i + 23 if i > 2
end

ARRAYS & LOOPS

Javascript

var myArray = [1,7,8];
var growingArray = [];
myArray.forEach(function(i) {
  if (i > 2) { growingArray.push(i + 23) }
});

ARRAYS & LOOPS

Swift

let myArray = [1,7,8]
var growingArray = [Int]()
myArray.forEach {(i: Int) in
  if i > 2 { growingArray.append(i + 23) }
}

ARRAYS & LOOPS

Ruby

class Frog < PondAnimal
  def initialize
    super
    self.sound = "ribbit"
  end
end

OOP (Object-Oriented Programming)

Javascript (ES6)

class Frog extends PondAnimal {
  constructor() {
    super();
    this.sound = "ribbit";
  }
}

OOP (Object-Oriented Programming)

note: the new OOP stuff in ES6 is really, really nice

Swift

class Frog: PondAnimal {
  override init() {
    super.init()
    self.sound = "ribbit"
  }
}

OOP (Object-Oriented Programming)

note: seeing a pattern here? Swift syntax is surprisingly easy on the eyes (most of the time)

Let's Run Some Code

Swift Ecosystem for Servers

Stencil looks like:

There are {{ articles.count }} articles.

{% for article in articles %}
  - {{ article.title }} by {{ article.author }}.
{% endfor %}

 

note: yep, this looks just like Liquid

Q. Is Swift ready for prime time?

A. Yes.

I think Apple's really gone all out in making Swift a true cross-platform, performant, general-purpose programming language with a touch of pizazz.

Well done.

Thank you.

Presented by Jared White for

North Bay App Developers

meetup.com/North-Bay-App-Developers/

@jaredcwhite

Swift for Ruby & Javascript Developers

By Jared White

Swift for Ruby & Javascript Developers

  • 460
Loading comments...

More from Jared White