Skip to content

TypeScript and JavaScript to Ruby

Recorder: the Node adapter (modules are imported; TypeScript is transpiled first). Frameworks: Express, Fastify, Koa, NestJS routes are found by analyze; React/Vue/Angular/Svelte/Next.js pages become ERB views (read frontend.md).

Constructs

  • Errors: throw new Error(m) -> raise m (RuntimeError); TypeError -> TypeError; RangeError -> RangeError (or ArgumentError); custom class X extends Error -> class X < StandardError. A rejected promise is a raise.
  • null / undefined -> nil. An object key holding undefined disappears from JSON.stringify; nil values stay as null in Rails' render json:. Use compact where the original relied on undefined keys vanishing.
  • Numbers: one number type. Integral results come back as ints in recordings; fractional ones as floats. Number.isInteger(5.0) is true. Return Integer where the recording shows ints.
  • Truthiness: 0, "", NaN, null, undefined are false in JS; only nil/false in Ruby. x || default -> x.presence || default for strings, explicit zero checks for numbers. ?? -> x.nil? ? default : x.
  • Arrays: map/filter/reduce/find/some/every/includes -> map/select/reduce/find/any?/all?/include?. arr.sort() without a comparator sorts as strings ([10, 9, 1].sort() == [1, 10, 9]): port that as sort_by(&:to_s). sort((a, b) => a - b) -> sort.
  • Objects keep insertion order, except integer-like keys, which JS puts first in ascending order. Spread {...a, ...b} -> a.merge(b). Optional chaining a?.b -> a&.dig(:b) / &..
  • Strings: length and indexes count UTF-16 code units: an emoji is 2. Ruby counts code points. Where emoji or astral characters reach a length, slice or index, recorded cases will show it; use s.encode("UTF-16LE").bytesize / 2 for length parity. substring swaps reversed arguments; slice takes negatives. toUpperCase is locale-free like upcase.
  • Express: req.params / req.query / req.body -> params; res.status(201).json(x) -> render json: x, status: :created; res.redirect(path) -> redirect_to path (302 in both); middleware -> before_action / Rack middleware; express.static -> public/.
  • Async: await chains become plain sequential calls; Promise.all over I/O -> sequential, or threads only when an ADR says latency matters.
  • Mongoose / Prisma / Sequelize / TypeORM -> ActiveRecord models; Mongo documents need a schema decision (ADR) or the mongoid gem.

Traps

  • Rounding: Math.round(-2.5) == -2 (half toward +infinity); Ruby -2.5.round == -3. Use (x + 0.5).floor. toFixed(2) rounds the binary value: (1.005).toFixed(2) == "1.00"; Ruby format("%.2f", 1.005) == "1.00" too, but 1.005.round(2) == 1.01: use format.
  • Integer division: 7 / 2 == 3.5; Math.floor(7 / 2) floors, Math.trunc truncates (negative numbers differ). % takes the dividend's sign (-7 % 2 == -1); Ruby takes the divisor's (1): use remainder.
  • Regex: no u flag means \w is ASCII (same as Ruby). /g with .test() keeps lastIndex state between calls: port the bug only if recorded.
  • Dates: new Date(y, m, d) months start at 0; Date.parse of a date-only string is UTC.
  • parseInt("12px") == 12; Ruby Integer("12px") raises: use "12px".to_i (but to_i returns 0 where parseInt returns NaN).
  • == coercion ("1" == 1): port the coercion explicitly where a recorded case depends on it.
Edit this page on GitHub →