Skip to content

Swift to Ruby

Recorder: the polyglot Swift adapter (swiftc + a generated driver). Free functions and static methods are recordable. Vapor routes are checked through app record/replay.

Constructs

  • throws: throw ValidationError.empty -> raise ValidationError, "empty" with a matching Ruby class per error enum, or ArgumentError for plain invalid input.
  • Optionals: String? -> nil; if let/guard let -> return ... if x.nil?; ?? -> .nil? ? d : x; force unwrap ! -> raise only where a recorded case shows it.
  • Structs/enums: value types -> Data.define; enums with associated values -> Data.define per case + case ... in; Codable keys decide JSON names (CodingKeys).
  • Collections: map/filter/reduce/compactMap/first(where:)/sorted(by:) -> map/select/reduce/filter_map/find/sort_by.
  • Vapor: app.get("x", ":id") { req in ... } -> route + action; req.parameters.get("id") -> params[:id]; Abort(.notFound) -> head :not_found with the recorded body.

Traps

  • Integer overflow traps (crashes) instead of wrapping; &+ wraps. / truncates toward zero (Ruby floors); % takes the dividend's sign (remainder).
  • Rounding: rounded() is half away from zero (Ruby round); .toNearestOrEven -> round(half: :even). String(format: "%.2f") -> format("%.2f").
  • Strings: count counts grapheme clusters (an emoji with a skin tone is 1); Ruby length counts code points: use s.grapheme_clusters.length. Indexing uses String.Index, not ints.
  • Dictionary order is random: sort where the output came from one.
Edit this page on GitHub →