PHP और Laravel से Ruby
Recorder: polyglot PHP adapter (functions और static methods एक generated driver के ज़रिए call किए जाते हैं)।
Laravel routes (Route::get) को analyze ढूँढ लेता है।
Laravel से Rails (दोनों काफ़ी मिलते-जुलते हैं)
| Laravel | Rails |
|---|---|
routes/web.php, routes/api.php |
config/routes.rb (/api prefix बनाए रखें) |
Controller + Request $request |
controller action + params |
return response()->json($x, 201) |
render json: x, status: :created |
abort(404) / firstOrFail() |
head :not_found / find_by! + recorded body के साथ rescue_from |
Blade (@extends, @section, @foreach, {{ }}) |
ERB layouts (yield/content_for), each, <%= %> (frontend.md) |
| Eloquent model, relations, scopes, casts | ActiveRecord model, associations, scopes, attribute/serializers |
| Migrations | Rails migrations, पर पहले मौजूदा tables ही रखें (ADR) |
Form Requests / $request->validate([...]) |
model validations या एक form object |
| Middleware | before_action / Rack middleware |
| Queues / Jobs | Active Job + Solid Queue |
| Mailables | Action Mailer |
config() / .env |
config/*.yml, credentials, ENV |
| Sanctum / Breeze auth | Rails authentication generator; bcrypt hashes साथ आ जाते हैं ($2y$ -> $2a$) |
Constructs
- Errors:
InvalidArgumentException->ArgumentError;DivisionByZeroError->ZeroDivisionError;RuntimeException->RuntimeError; app के अपने exceptions अपने नाम बनाए रखते हैं। - Arrays ordered maps हैं:
0..n-1keys वाला PHP array JSON list होता है; gaps होने पर (unsetयाarray_filterके बाद) वह JSON object बन जाता है। जहाँ recording list दिखाए, वहाँarray_valuessemantics (values) इस्तेमाल करें। - null ->
nil;isset($a['k'])->a.key?("k") && !a["k"].nil?;empty($x)इनके लिए true है:0,"0","",[],null। - Strings:
strlenbytes गिनता है (bytesize);mb_strlencharacters गिनता है (length)।.concatenation -> interpolation।
Traps
- Loose comparison (
==, strict के बिनाin_array):"1" == 1,"abc" == 0(PHP 7)। जहाँ कोई recorded case इस coercion पर निर्भर हो, वहाँ इसे explicitly port करें। - Rounding:
round(2.5) == 3औरround(-2.5) == -3(half away from zero), Ruby केroundजैसा।intdivzero की ओर truncate करता है;%dividend का sign लेता है (remainder);/exact होने पर int लौटाता है (4 / 2 === 2) और बाकी समय float। - Int overflow चुपचाप float में बदल जाता है।
json_encodedefault रूप से/को\/और unicode को\uXXXXके रूप में escape करता है: body text अलग होता है पर parsed JSON बराबर होता है (replay parsed JSON की तुलना करता है)।- PHP 8 से पहले
sort,usortstable नहीं हैं।