Go से Ruby
Recorder: polyglot Go adapter (हर function के लिए एक generated driver, go build)। Frameworks:
net/http, Gin, Echo, Fiber और chi routes को analyze ढूँढ लेता है।
Constructs
- Errors as values:
return 0, fmt.Errorf("bad input %q", s)->raise ArgumentError, ...(recorder किसी returned non-nil error को raise के रूप में report करता है)। Sentinel errors (var ErrNotFound = errors.New(...)) ->class NotFound < StandardError।panic->raise(RuntimeError)। - Multiple returns -> एक array या छोटा
Data.define, जब तक दूसरी value error न हो। - Zero values: unset
int0होता है,string"", nil slice JSON मेंnullबनता है जबकि खाली slice[]बनता है। Recorded JSON से हूबहू मिलाएँ। - Structs ->
Data.define/Struct; JSON tags (json:"user_id,omitempty") keys तय करते हैं और यह भी कि zero values हटें या नहीं:omitemptyकोcompact/explicit checks से port करें। - Maps: Go में iteration order random होता है; recorder sorted या noisy order दिखाता है। जहाँ भी original का output किसी map से आया हो, output से पहले sort करें।
- Goroutines/channels -> sequential code, या Active Job जब वे background काम थे।
- Handlers:
w.WriteHeader(201); json.NewEncoder(w).Encode(x)->render json: x, status: :created। Ginc.JSON(200, gin.H{...})->render json: {...}।http.Error(w, msg, 400)msg + "\n"को text/plain के रूप में लिखता है:render plain: "#{msg}\n", status: :bad_request। - database/sql, sqlx, GORM -> उन्हीं tables पर ActiveRecord।
Traps
- Integer types:
int32/int64/uint8overflow होकर wrap होते हैं; Ruby में ऐसा कभी नहीं होता। Wrap सिर्फ़ वहीं port करें जहाँ कोई recorded case उस तक पहुँचे। Integer division zero की ओर truncate करता है (-7 / 2 == -3): Ruby floor करता है;.fdiv(b).truncateइस्तेमाल करें।%dividend का sign लेता है:remainder। - Rounding:
math.Round(-2.5) == -3(half away from zero), Ruby केroundजैसा।math.RoundToEven->round(half: :even)।fmt.Sprintf("%.2f")->format("%.2f")। - Strings bytes हैं:
len(s)bytes गिनता है,s[i]एक byte है,for i, r := range srunes पर चलता है। Ruby काlengthcharacters गिनता है: जहाँ original bytes इस्तेमाल करता है वहाँbytesize/bytesliceइस्तेमाल करें। strings.Title,strings.ToUpperRuby केupcaseकी तरह Unicode-aware हैं;strings.Fields->split(बिना argument)।- JSON में floats: 1.0 रखने वाले float64 के लिए Go
1लिखता है; recordings में वह int दिखता है। - time.Time nanoseconds के साथ RFC 3339 के रूप में marshal होता है:
iso8601(9)और Go की तरह trailing zeros हटाएँ।