TypeScript और JavaScript से Ruby
Recorder: Node adapter (modules import किए जाते हैं; TypeScript पहले transpile होता है)। Frameworks:
Express, Fastify, Koa, NestJS routes को analyze ढूँढ लेता है; React/Vue/Angular/Svelte/Next.js pages
ERB views बनते हैं (frontend.md पढ़ें)।
Constructs
- Errors:
throw new Error(m)->raise m(RuntimeError);TypeError->TypeError;RangeError->RangeError(या ArgumentError); customclass X extends Error->class X < StandardError। Rejected promise एक raise है। - null / undefined ->
nil।undefinedरखने वाली object keyJSON.stringifyसे गायब हो जाती है; Rails केrender json:मेंnilvaluesnullके रूप में बनी रहती हैं। जहाँ original undefined keys के गायब होने पर निर्भर था, वहाँcompactइस्तेमाल करें। - Numbers: सिर्फ़ एक
numbertype। Recordings में integral results ints के रूप में आते हैं; fractional वाले floats के रूप में।Number.isInteger(5.0)true है। जहाँ recording ints दिखाए, वहाँIntegerलौटाएँ। - Truthiness: JS में
0,"",NaN,null,undefinedfalse हैं; Ruby में सिर्फ़nil/false।x || default-> strings के लिएx.presence || default, numbers के लिए explicit zero checks।??->x.nil? ? default : x। - Arrays:
map/filter/reduce/find/some/every/includes->map/select/reduce/find/any?/all?/include?। बिना comparator केarr.sort()strings की तरह sort करता है ([10, 9, 1].sort() == [1, 10, 9]): इसेsort_by(&:to_s)के रूप में port करें।sort((a, b) => a - b)->sort। - Objects insertion order रखते हैं, सिवाय integer जैसी keys के, जिन्हें JS ascending order में सबसे पहले
रखता है। Spread
{...a, ...b}->a.merge(b)। Optional chaininga?.b->a&.dig(:b)/&.। - Strings:
lengthऔर indexes UTF-16 code units गिनते हैं: एक emoji 2 होता है। Ruby code points गिनता है। जहाँ emoji या astral characters किसी length, slice या index तक पहुँचते हैं, recorded cases उसे दिखाएँगे; length की बराबरी के लिएs.encode("UTF-16LE").bytesize / 2इस्तेमाल करें।substringउल्टे arguments को अदल-बदल देता है;slicenegatives लेता है।toUpperCaseupcaseकी तरह locale-free है। - Express:
req.params/req.query/req.body->params;res.status(201).json(x)->render json: x, status: :created;res.redirect(path)->redirect_to path(दोनों में 302); middleware ->before_action/ Rack middleware;express.static->public/। - Async:
awaitchains साधारण sequential calls बन जाती हैं; I/O परPromise.all-> sequential, या threads सिर्फ़ तब जब कोई ADR कहे कि latency मायने रखती है। - Mongoose / Prisma / Sequelize / TypeORM -> ActiveRecord models; Mongo documents के लिए schema का फ़ैसला (ADR) या mongoid gem चाहिए।
Traps
- Rounding:
Math.round(-2.5) == -2(half, +infinity की ओर); Ruby में-2.5.round == -3।(x + 0.5).floorइस्तेमाल करें।toFixed(2)binary value को round करता है:(1.005).toFixed(2) == "1.00"; Ruby मेंformat("%.2f", 1.005) == "1.00"भी, पर1.005.round(2) == 1.01:formatइस्तेमाल करें। - Integer division:
7 / 2 == 3.5;Math.floor(7 / 2)floor करता है,Math.trunctruncate करता है (negative numbers में फ़र्क आता है)।%dividend का sign लेता है (-7 % 2 == -1); Ruby divisor का (1):remainderइस्तेमाल करें। - Regex:
uflag के बिना\wASCII है (Ruby जैसा)।.test()के साथ/gcalls के बीचlastIndexstate रखता है: यह bug सिर्फ़ तब port करें जब record हुआ हो। - Dates:
new Date(y, m, d)में months 0 से शुरू होते हैं; date-only string काDate.parseUTC होता है। - parseInt("12px") == 12; Ruby का
Integer("12px")raise करता है:"12px".to_iइस्तेमाल करें (पर जहाँparseIntNaNलौटाता है, वहाँto_i0 लौटाता है)। ==coercion ("1" == 1): जहाँ कोई recorded case इस coercion पर निर्भर हो, वहाँ इसे explicitly port करें।