Skip to content

Kotlin to Ruby

Recorder: the polyglot Kotlin adapter (kotlinc + the Java driver). Top-level functions are recordable. Ktor and Spring (Kotlin) routes are found by analyze; read java.md for Spring.

Constructs

  • Null safety: String? -> may be nil; ?. -> &.; ?: -> || (when false is not a value) or .nil? ? d : x; !! -> raise NoMethodError-like errors only where a recorded case shows it.
  • Errors: require(cond) { msg } -> raise ArgumentError, msg unless cond; check() -> RuntimeError (IllegalStateException); error(msg) -> raise msg.
  • Data classes -> Data.define (value equality, with for copy).
  • Sealed classes / when -> a case ... in pattern match or case on class.
  • Collections: map/filter/fold/groupBy/associateBy/sumOf/joinToString -> map/select/reduce/group_by/index_by/sum/join. listOf is immutable: freeze if mutation would be a bug.
  • Ktor: get("/x/{id}") { call.respond(HttpStatusCode.OK, x) } -> route + render json:.

Traps

  • Same numeric traps as Java: Int overflow wraps, / truncates toward zero, % takes the dividend's sign (remainder), roundToInt() rounds half up toward +infinity (2.5 -> 3, -2.5 -> -2).
  • String.length counts UTF-16 units; substring throws StringIndexOutOfBoundsException (-> IndexError) where Ruby returns nil.
  • toInt() on a string raises NumberFormatException (-> ArgumentError, like Integer(s)); toIntOrNull() -> Integer(s, exception: false).
Edit this page on GitHub →