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, orArgumentErrorfor 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.defineper case +case ... in;Codablekeys 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_foundwith 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 (Rubyround);.toNearestOrEven->round(half: :even).String(format: "%.2f")->format("%.2f"). - Strings:
countcounts grapheme clusters (an emoji with a skin tone is 1); Rubylengthcounts code points: uses.grapheme_clusters.length. Indexing usesString.Index, not ints. - Dictionary order is random: sort where the output came from one.