Python से Ruby
Recorder: Python adapter (functions import करके call किए जाते हैं; top-level code चलता है)।
Frameworks: Flask, FastAPI और Django routes को analyze ढूँढ लेता है।
Constructs
- Errors:
raise ValueError(...)->raise ArgumentError, "...";KeyError->KeyError;IndexError->IndexError;TypeError->TypeError; app की अपनी exception classes -> उसी नाम के साथclass Name < StandardError। - None ->
nil;Optional[T]parameters का defaultnilहोता है।x or defaultतभी एक जैसा चलता है जबxकभी0,"",[]या{}न हो: Python इन्हें false मानता है, Ruby नहीं।x.presence || defaultया explicit checks इस्तेमाल करें। - Truthiness:
if items:->if items.any?(याunless items.empty?);if s:->unless s.empty?;if n:->unless n.zero?। - Comprehensions ->
map/select/filter_map/each_with_object;dict(zip(a, b))->a.zip(b).to_h;enumerate->each_with_index;sorted(x, key=f)->sort_by(दोनों में stable);sum->sum। - Strings:
len, slicing और indexes Ruby केString#length/[]की तरह code points गिनते हैं;s[a:b]->s[a...b];s[-1]एक जैसा चलता है; f-strings -> interpolation; बिना argument केstr.split()->split(दोनों whitespace को समेट देते हैं) परsplit(" ")अलग है: Python खाली strings रखता है, Ruby काsplit(" ")awk-style है;split(/ /, -1)इस्तेमाल करें। - Dicts Ruby hashes की तरह insertion order रखते हैं। JSON तक पहुँचने पर string keys strings ही रहती हैं (
"name"), symbols नहीं। - Classes: dataclasses ->
Struct/Data.defineया साधारण classes;@property-> reader methods;__eq__/__hash__->==/eql?/hash। - Django/Flask: views -> controller actions;
request.args->params;jsonify(x)->render json: x;abort(404)->head :not_found(या original की error body); Django ORM -> ActiveRecord (filter->where,exclude->where.not,get->find_by!,select_related->includes)।
Traps
- Rounding:
round(2.5) == 2(half to even)। Ruby में2.5.round == 3।round(half: :even)इस्तेमाल करें।round(x, 2)भी binary value पर half to even round करता है:round(2.675, 2) == 2.67। - Division:
/हमेशा float लौटाता है (7 / 2 == 3.5,4 / 2 == 2.0); Ruby में7 / 2 == 3।fdivया/ 2.0इस्तेमाल करें।//floor करता है:-7 // 2 == -4, Ruby के-7 / 2जैसा। - Modulo दोनों भाषाओं में divisor का sign लेता है (
-7 % 2 == 1): एक जैसा। - Ints बनाम floats:
diff2और2.0में फ़र्क करता है;x / 2लौटाने वाला function evenxके लिए भी floats लौटाता है। - Big ints किसी भी भाषा में overflow नहीं होते: एक जैसा।
str.isalpha/isdigitUnicode-aware हैं; Ruby का/[[:alpha:]]/भी है,/[a-z]/नहीं।- Regex: Python के
\w,\dUnicode letters और digits से match करते हैं; Ruby के सिर्फ़ ASCII से। जब original non-ASCII text देखता हो, तो\p{Word}/\p{Nd}या[[:alnum:]_]इस्तेमाल करें।re.matchसिर्फ़ शुरुआत पर anchor करता है (Ruby में\A);re.fullmatch->\A...\z। - Mixed types को sort करना Python में
TypeErrorraise करता है; RubyArgumentErrorraise करता है। - Default mutable arguments (
def f(x=[])) calls के बीच state share करते हैं; यह bug सिर्फ़ तब port करें जब कोई recorded case इसे दिखाए। - Clock और random: इन्हें pin करें (time को argument में पास करें), या wildcards के बारे में user से पूछें।