Skip to content

Swift에서 Ruby로

Recorder: polyglot Swift 어댑터(swiftc + 생성된 드라이버). 자유 함수와 static 메서드를 기록할 수 있습니다. Vapor 라우트는 app record/replay로 검증합니다.

구문

  • throws: throw ValidationError.empty -> raise ValidationError, "empty". 에러 enum마다 대응하는 Ruby 클래스를 두고, 단순히 잘못된 입력이라면 ArgumentError를 쓰세요.
  • 옵셔널: String? -> nil, if let/guard let -> return ... if x.nil?, ?? -> .nil? ? d : x, 강제 언래핑 ! -> 기록된 케이스에 나타날 때만 raise.
  • 구조체/enum: 값 타입 -> Data.define, 연관 값을 가진 enum -> case마다 Data.define + case ... in. Codable 키(CodingKeys)가 JSON 이름을 결정합니다.
  • 컬렉션: map/filter/reduce/compactMap/first(where:)/sorted(by:) -> map/select/reduce/filter_map/find/sort_by.
  • Vapor: app.get("x", ":id") { req in ... } -> 라우트 + 액션, req.parameters.get("id") -> params[:id], Abort(.notFound) -> 기록된 본문을 돌려주는 head :not_found.

함정

  • 정수 오버플로는 순환하지 않고 트랩(크래시)이 발생합니다. &+는 순환합니다. /는 0 쪽으로 버리고(Ruby는 내림), %는 피제수의 부호를 따릅니다(remainder).
  • 반올림: rounded()는 0.5를 0에서 먼 쪽으로 반올림합니다(Ruby의 round). .toNearestOrEven -> round(half: :even). String(format: "%.2f") -> format("%.2f").
  • 문자열: count는 grapheme cluster를 셉니다(피부색이 적용된 이모지는 1). Ruby의 length는 코드 포인트를 세므로 s.grapheme_clusters.length를 쓰세요. 인덱싱에는 int가 아닌 String.Index를 씁니다.
  • Dictionary의 순서는 무작위입니다. 출력이 Dictionary에서 나왔다면 정렬하세요.
GitHub에서 이 페이지 수정하기 →