Swift is like Ruby
Shamefully forked from Swift is like Go
BASICS
Hello World
Swift
            print("Hello, world!")
          
Ruby
            puts 'Hello, world!'
          
Variables And Constants
Swift
            
var myVariable = 42
myVariable = 50
let myConstant = 42
            
          
Ruby
            
my_variable = 42
my_variable = 50
MY_CONSTANT = 42
            
          
Explicit Types
Swift
              let explicitDouble: Double = 70
            
Ruby
              explicit_double = 70.0
            
Type Coercion
Swift
              
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
              
            
Ruby
              
label = "The width is "
width = 94
widthLabel = label + width.to_s
              
            
String Interpolation
Swift
              
let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
                   "pieces of fruit."
              
            
Ruby
              
apples = 3
oranges = 5
fruit_summary = "I have #{apples + oranges} " <<
                   "pieces of fruit."
              
            
Range Operator
Swift
              
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..count {
    print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
              
            
Ruby
              
names = %w(Anna Alex Brian Jack)
count = names.size
(0...count).each do |i|
  puts "Person #{i + 1} is called #{names[i]}"
end
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
              
            
Inclusive Range Operator
Swift
              
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
              
            
Ruby
              
(1..5).each do |index|
  puts "#{index} times 5 is #{index * 5}"
end
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
              
            
BASICS
Arrays
Swift
              
var shoppingList = ["catfish", "water",
    "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
              
            
Ruby
              
shopping_list = ["catfish", "water",
  "tulips", "blue paint"]
shopping_list[1] = "bottle of water"
              
            
Dictionaries
Swift
              
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
for (key, value) in occupations {
    print(key)
}
              
            
Ruby
              
occupations = {
  "Malcolm" => "Captain",
  "Kaylee" => "Mechanic"
}
occupations["Jayne"] = "Public Relations"
occupations.each do |key, value|
  print(key)
end
              
            
Empty Collections
Swift
              
var emptyArray: [String] = []
var emptyDictionary: [String: Float] = [:]
              
            
Ruby
              
empty_array = []
empty_map = {}
              
            
FUNCTIONS
Functions
Swift
              
func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
              
            
Ruby
              
def greet(name, day)
  "Hello #{name}, today is #{day}."
end
greet("Bob", "Tuesday")
              
            
Tuple Return
Swift
              
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
              
            
Ruby
              
def gas_prices
  [3.59, 3.69, 3.79]
end
              
            
Variable Number Of Arguments
Swift
              
func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf(42, 597, 12)
              
            
Ruby
              
def sum_of(*numbers)
  sum = 0
  for number in numbers
    sum += number
  end
  sum
end
sum_of(42, 597, 12)

// the body of the function can be written just as:
numbers.reduce &:+
              
            
Function Type
Swift
              
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)
              
            
Ruby
              
def make_incrementer
  def add_one number
    1 + number
  end
end
increment = make_incrementer
increment(7)

// makeIncrementer() can also be written in a shorter way:
make_incrementer = lambda { |number| 1 + number }
              
            
Map
Swift
              
var numbers = [20, 19, 7, 12]
numbers.map({ number in 3 * number })
              
            
Ruby
              
numbers = [20, 19, 7, 12]
numbers.map { |number| 3 * number }
              
            
Sort
Swift
              sort([1, 5, 3, 12, 2]) { $0 > $1 }
            
Ruby
              [1, 5, 3, 12, 2].sort
            
Named Arguments
Swift
              
def area(width: Int, height: Int)
    return width * height
}

area(width: 10, height: 10)
              
            
Ruby
              
def area(width:, height:)
  width * height
end

area(height: 10, width: 10)
              
            
CLASSES
Declaration
Swift
              
class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}
              
            
Ruby
              
class Shape
  attr_writer :number_of_sides
  def initialize
    @number_of_sides = 0
  end

  def simple_description
    "A shape with #{@number_of_sides} sides."
  end
end
              
            
Usage
Swift
              
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
              
            
Ruby
              
shape = Shape.new
shape.number_of_sides = 7
shape_description = shape.simple_description
              
            
Subclass
Swift
              
class NamedShape {
    var numberOfSides: Int = 0
    var name: String

    init(name: String) {
        self.name = name
    }

    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

class Square: NamedShape {
    var sideLength: Double

    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        numberOfSides = 4
    }

    func area() -> Double {
        return sideLength * sideLength
    }

    override func simpleDescription() -> String {
        return "A square with sides of length \(sideLength)."
    }
}

let test = Square(sideLength: 5.2)
test.area()
test.simpleDescription()
              
            
Ruby
              
class NamedShape
  attr_writer :number_of_sides
  def initialize(name)
    @name = name
    @number_of_sides = 0
  end
  def simpleDescription
    "A shape with #{@number_of_sides} sides."
  end
end

class Square < NamedShape
  def initialize(side_length, name = '')
    @side_length = side_length
    @number_of_sides = 4
    super name
  end

  def area
    @side_length ** 2
  end

  def simple_description
    "A square with sides of length #{@side_length}."
  end
end

test = Square.new 5.2
test.area
test.simple_description
              
            
Checking Type
Swift
              
var movieCount = 0
var songCount = 0

for item in library {
    if item is Movie {
        ++movieCount
    } else if item is Song {
        ++songCount
    }
}
              
            
Ruby
              
movie_count = 0
song_count = 0

library.each do |item|
  if item.is_a? Movie
    movie_count += 1
  elsif item.is_a? Song
    song_count += 1
  end
end
              
            
Extensions
Swift
              
extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
              
            
Ruby
              
class Float
  def km
    self * 1000
  end
  def m
    self
  end
  def cm
    self / 100
  end
  def mm
    self / 1000
  end
  def ft
    self / 3.28084
  end
end

one_inch = 25.4.mm
puts "One inch is #{one_inch} meters"
# prints "One inch is 0.0254 meters"
three_feet = 3.0.ft
puts "Three feet is #{three_feet} meters"
# prints "Three feet is 0.914399970739201 meters"