Skip to content

Java और Spring Boot से Ruby

Recorder: polyglot Java adapter (javac + एक generated driver)। public static methods और stateless classes के public methods (कोई instance fields नहीं, कोई constructor arguments नहीं, आम @Service/@Component helpers) record किए जा सकते हैं; recording के लिए Spring annotations हटा दी जाती हैं, और target/dependency में मौजूद dependency jars (mvn dependency:copy-dependencies) classpath पर जाते हैं। Injected dependencies वाली classes की जाँच app record/replay से होती है।

Spring से 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 उसी body और status के साथ ApplicationController में rescue_from
@Service app/services में service object
@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; bcrypt hashes बनाए रखें
application.properties / @Value config/*.yml, credentials, ENV.fetch
@Scheduled Solid Queue recurring jobs (config/recurring.yml)
Jackson field names (camelCase) render json: में camelCase keys बनाए रखें (key transform के साथ as_json या explicit hashes)

Constructs

  • Errors: IllegalArgumentException -> ArgumentError; IllegalStateException -> RuntimeError; NoSuchElementException (Optional.get, orElseThrow) -> KeyError या app का अपना error; NullPointerException -> NoMethodError (इसे सिर्फ़ तब port करें जब कोई recorded case इसे दिखाए)।
  • Optional -> nil handling; orElse(d) -> || d (या .nil? ? d : x जब false भी एक 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 / attr_reader वाली साधारण classes।
  • Overloads -> optional या keyword arguments; हर overload के recorded cases जाँचें।

Traps

  • Integer overflow: int 2^31 पर wrap होता है (Integer.MAX_VALUE + 1 negative है); long 2^63 पर। Ruby integers कभी overflow नहीं होते। Wrap (((x + 2**31) % 2**32) - 2**31) सिर्फ़ वहीं port करें जहाँ कोई recorded case उस तक पहुँचे।
  • Integer division zero की ओर truncate करता है (-7 / 2 == -3); Ruby floor करता है (-4)। (a.fdiv(b)).truncate या a.quo(b).truncate इस्तेमाल करें। % dividend का sign लेता है: remainder इस्तेमाल करें।
  • Rounding: Math.round(-2.5) == -2 (half up, +infinity की ओर); RoundingMode.HALF_EVEN वाला BigDecimal -> round(half: :even); HALF_UP -> round(half: :up)।
  • BigDecimal money -> Ruby BigDecimal (require "bigdecimal"); पैसों के लिए कभी Float नहीं।
  • Strings: length() JS की तरह UTF-16 units गिनता है। String.format("%.2f") -> format("%.2f")। equals बनाम ==: Ruby का == values की तुलना करता है।
  • Regex: Java में \w ASCII है जब तक UNICODE_CHARACTER_CLASS न हो: Ruby जैसा ही। matches() पूरी string को anchor करता है (\A...\z); find() नहीं करता।
  • HashMap का iteration order तय नहीं है; LinkedHashMap Ruby hashes की तरह insertion order रखता है; TreeMap sorted होता है (sort.to_h)। जहाँ original ने HashMap इस्तेमाल किया हो और कोई recorded case कोई order दिखाए, वहाँ output से पहले sort करें।
इस page को GitHub पर edit करें →