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) ->KeyErroror the app's own error;NullPointerException->NoMethodError(port it only when a recorded case shows it). - Optional ->
nilhandling;orElse(d)->|| d(or.nil? ? d : xwhen 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 withattr_reader. - Overloads -> optional or keyword arguments; check every overload's recorded cases.
Traps
- Integer overflow:
intwraps at 2^31 (Integer.MAX_VALUE + 1is negative);longat 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)).truncateora.quo(b).truncate.%takes the dividend's sign: useremainder. - Rounding:
Math.round(-2.5) == -2(half up toward +infinity);BigDecimalwithRoundingMode.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").equalsvs==: Ruby==compares values. - Regex: Java
\wis ASCII unlessUNICODE_CHARACTER_CLASS: same as Ruby.matches()anchors the whole string (\A...\z);find()does not. - HashMap iteration order is unspecified;
LinkedHashMapkeeps insertion order like Ruby hashes;TreeMapis sorted (sort.to_h). Sort before output where the original usedHashMapand a recorded case shows an order.