Skip to content

Java and Spring Boot to Ruby

Recorder: the polyglot Java adapter (javac + a generated driver). public static methods and public methods of stateless classes (no instance fields, no constructor arguments, typical @Service/@Component helpers) are recordable; Spring annotations are stripped for the recording, and dependency jars in target/dependency (mvn dependency:copy-dependencies) go on the classpath. Classes with injected dependencies are checked through app record/replay.

Spring to Rails

Spring Rails
@RestController + @GetMapping("/x/{id}") get "x/:id" + controller action, render json:
@Controller + Thymeleaf template controller action + ERB view (frontend.md)
@PathVariable, @RequestParam, @RequestBody params[:id], params[:q], params.expect(...) / params.require().permit()
ResponseEntity.status(201).body(x) render json: x, status: :created
@ResponseStatus(NOT_FOUND) exception / @ControllerAdvice rescue_from in ApplicationController with the same body and status
@Service service object in app/services
@Entity + JpaRepository ActiveRecord model; derived queries (findByEmail) -> find_by(email:); @Query -> scopes / where
@Transactional ActiveRecord::Base.transaction do ... end
Bean Validation (@NotBlank, @Size, @Email) validates :x, presence:, length:, format:
Spring Security Rails authentication generator + before_action; keep bcrypt hashes
application.properties / @Value config/*.yml, credentials, ENV.fetch
@Scheduled Solid Queue recurring jobs (config/recurring.yml)
Jackson field names (camelCase) keep camelCase keys in render json: (as_json with a key transform or explicit hashes)

Constructs

  • Errors: IllegalArgumentException -> ArgumentError; IllegalStateException -> RuntimeError; NoSuchElementException (Optional.get, orElseThrow) -> KeyError or the app's own error; NullPointerException -> NoMethodError (port it only when a recorded case shows it).
  • Optional -> nil handling; orElse(d) -> || d (or .nil? ? d : x when false is a value).
  • Streams: stream().filter().map().collect(toList()) -> select.map; Collectors.groupingBy -> group_by; joining(", ") -> join(", "); sorted(comparing(X::y)) -> sort_by(&:y).
  • Records / POJOs -> Data.define / Struct / plain classes with attr_reader.
  • Overloads -> optional or keyword arguments; check every overload's recorded cases.

Traps

  • Integer overflow: int wraps at 2^31 (Integer.MAX_VALUE + 1 is negative); long at 2^63. Ruby integers never overflow. Port the wrap (((x + 2**31) % 2**32) - 2**31) only where a recorded case reaches it.
  • Integer division truncates toward zero (-7 / 2 == -3); Ruby floors (-4). Use (a.fdiv(b)).truncate or a.quo(b).truncate. % takes the dividend's sign: use remainder.
  • Rounding: Math.round(-2.5) == -2 (half up toward +infinity); BigDecimal with RoundingMode.HALF_EVEN -> round(half: :even); HALF_UP -> round(half: :up).
  • BigDecimal money -> Ruby BigDecimal (require "bigdecimal"); never Float for money.
  • Strings: length() counts UTF-16 units like JS. String.format("%.2f") -> format("%.2f"). equals vs ==: Ruby == compares values.
  • Regex: Java \w is ASCII unless UNICODE_CHARACTER_CLASS: same as Ruby. matches() anchors the whole string (\A...\z); find() does not.
  • HashMap iteration order is unspecified; LinkedHashMap keeps insertion order like Ruby hashes; TreeMap is sorted (sort.to_h). Sort before output where the original used HashMap and a recorded case shows an order.
Edit this page on GitHub →