Skip to content

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 का default nil होता है। 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: diff 2 और 2.0 में फ़र्क करता है; x / 2 लौटाने वाला function even x के लिए भी floats लौटाता है।
  • Big ints किसी भी भाषा में overflow नहीं होते: एक जैसा।
  • str.isalpha/isdigit Unicode-aware हैं; Ruby का /[[:alpha:]]/ भी है, /[a-z]/ नहीं।
  • Regex: Python के \w, \d Unicode 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 में TypeError raise करता है; Ruby ArgumentError raise करता है।
  • Default mutable arguments (def f(x=[])) calls के बीच state share करते हैं; यह bug सिर्फ़ तब port करें जब कोई recorded case इसे दिखाए।
  • Clock और random: इन्हें pin करें (time को argument में पास करें), या wildcards के बारे में user से पूछें।
इस page को GitHub पर edit करें →