Commit 2874f417

2874f4170de9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Implement Array#join for poly_array

A mixed-element array (poly_array) had no #join, so [1, "x", :y].join(",") raised undefined method 'join' for poly_array at runtime; the homogeneous int_array/str_array specializations have typed join helpers. Add sp_PolyArray_join, which to_s's each element via sp_poly_to_s and concatenates with the separator, and wire it into the poly_array dispatch.

Found via the spinelgems harness (heterogeneous-array string builders).

Closes #1332

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
2874f4170de9
Parent
9384d37c3bb0

lib/sp_runtime.hmodifiedView file

@@ -3237,6 +3237,18 @@
32373237 sp_String_append(s, "]");
32383238 return s->data;
32393239 }
3240+/* Array#join for a mixed-element (poly) array: to_s each element via
3241+ sp_poly_to_s and concatenate with sep. Mirrors sp_StrArray_join for
3242+ the boxed-element case. */
3243+static const char *sp_PolyArray_join(sp_PolyArray *a, const char *sep) {
3244+ if (!a) return sp_str_empty;
3245+ sp_String *s = sp_String_new("");
3246+ for (mrb_int i = 0; i < a->len; i++) {
3247+ if (i > 0 && sep) sp_String_append(s, sep);
3248+ sp_String_append(s, sp_poly_to_s(a->data[i]));
3249+ }
3250+ return s->data;
3251+}
32403252 static mrb_bool sp_PolyArray_eq(sp_PolyArray *a, sp_PolyArray *b) {
32413253 if (!a || !b) return a == b;
32423254 if (a->len != b->len) return FALSE;