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 ->
nilhandling;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:
int2^31 पर wrap होता है (Integer.MAX_VALUE + 1negative है);long2^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 में
\wASCII है जब तकUNICODE_CHARACTER_CLASSन हो: Ruby जैसा ही।matches()पूरी string को anchor करता है (\A...\z);find()नहीं करता। - HashMap का iteration order तय नहीं है;
LinkedHashMapRuby hashes की तरह insertion order रखता है;TreeMapsorted होता है (sort.to_h)। जहाँ original नेHashMapइस्तेमाल किया हो और कोई recorded case कोई order दिखाए, वहाँ output से पहले sort करें।