Swift for Ruby Developers
Move from Ruby to Swift through static types, value semantics, optionals, Unicode, protocols and ARC. Complete 48 multi-file exercises, build a parcel desk and port two complete Ruby programs.
Ruby and Swift side by side
The same program in Ruby and in Swift, from the lessons of this course.
Bindings and Mutation
Ruby local variables can be rebound to objects of different types. Reassigning label to an integer does not change saved. Ruby String objects can be mutable, so replacing a binding and mutating a shared string object are separate operations; this example only…
Ruby1label = "draft"2saved = label3label = 174puts "#{saved} #{label}"Swift1let saved = "draft"2var label = saved3label = "ready"4print(saved, label)Strings and Interpolation
Ruby double-quoted strings interpolate expressions with #{...}. Methods such as start_with? answer text questions directly, and multiplying a string by an integer repeats it. Ruby strings can be mutable objects with an encoding, so object sharing and…
Ruby1done = 32puts "progress=#{done * 25}%"3puts "report.csv".start_with?("report")Swift1let done = 32print("progress=\(done * 25)%")3print("report.csv".hasPrefix("report"))Switch and Patterns
Ruby case/when uses each condition's === behavior, so patterns such as ranges or classes can act differently from simple equality. Multiple when values can share a branch. Case does not automatically fall through to the next branch, and an omitted match can…
Ruby1command = "pause"2case command3when "pause", "stop" then puts "idle"4else puts "running"5endSwift1let command = "pause"2switch command {3case "pause", "stop": print("idle")4default: print("running")5}Functions and Labels
Ruby distinguishes positional and keyword parameters. Times is a required keyword here, and the method's final expression supplies its return value. A Ruby keyword name is the local parameter name, while Swift can give the caller a different external label…
Ruby1def echo(sound, times:)2 sound * times3end4puts echo("ha", times: 3)Swift1func echo(_ sound: String, times count: Int) -> String {2 return String(repeating: sound, count: count)3}Dictionaries and Missing Keys
Ruby Hash#delete removes an entry, while assigning nil preserves a key with a nil value. Fetch can distinguish a missing entry from that present nil. Hashes preserve insertion order, but an alphabetical report still needs sorting; insertion history is a…
Ruby1settings = {"font" => 14, "gap" => 2}2settings.delete("gap")3puts settings.fetch("gap", 8)4puts settings.lengthSwift1var settings = ["font": 14, "gap": 2]2settings["gap"] = nil3print(settings["gap"] ?? 8)4print(settings.count)Structs and Independent Copies
Ruby Struct creates a class of record-like objects; it is not a Swift value-type struct. Assigning a Struct instance to another local aliases that instance, so changing direction affects both names. Dup can create a shallow separate instance, but referenced…
Ruby1Heading = Struct.new(:direction)2first = Heading.new("north")3second = first4second.direction = "west"5puts "#{first.direction} #{second.direction}"Swift1struct Heading {2 var direction: String3 mutating func turnWest() { direction = "west" }4}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsRuby Ports
2 lessonsFrequently asked questions
Who is Swift for Ruby Developers for?
For people who already write Ruby and want to learn Swift. Each lesson starts from Ruby you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need Ruby basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for Ruby Developers have?
Swift for Ruby Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for Ruby Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for Ruby Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.