Skip to content

Swift से Ruby

Recorder: polyglot Swift adapter (swiftc + एक generated driver)। Free functions और static methods record किए जा सकते हैं। Vapor routes की जाँच app record/replay से होती है।

Constructs

  • throws: throw ValidationError.empty -> raise ValidationError, "empty", हर error enum के लिए मेल खाती Ruby class के साथ, या साधारण invalid input के लिए ArgumentError।
  • Optionals: String? -> nil; if let/guard let -> return ... if x.nil?; ?? -> .nil? ? d : x; force unwrap ! -> सिर्फ़ वहीं raise करें जहाँ कोई recorded case उसे दिखाए।
  • Structs/enums: value types -> Data.define; associated values वाले enums -> हर case के लिए Data.define + case ... in; Codable keys 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) -> recorded body के साथ head :not_found।

Traps

  • Integer overflow wrap होने के बजाय trap (crash) करता है; &+ wrap करता है। / zero की ओर truncate करता है (Ruby floor करता है); % dividend का sign लेता है (remainder)।
  • Rounding: rounded() half away from zero है (Ruby का round); .toNearestOrEven -> round(half: :even)। String(format: "%.2f") -> format("%.2f")।
  • Strings: count grapheme clusters गिनता है (skin tone वाला emoji 1 है); Ruby का length code points गिनता है: s.grapheme_clusters.length इस्तेमाल करें। Indexing में String.Index लगता है, ints नहीं।
  • Dictionary का order random होता है: जहाँ output उससे आया हो वहाँ sort करें।
इस page को GitHub पर edit करें →