Skip to content

Array: #[]= auto-grow + range/subclass assign, values_at ranges, dig/product/insert/rotate/flatten to_int (MRI 4.0.6) - #538

Merged
tannevaled merged 4 commits into
mainfrom
feat/agent-arraymut
Aug 31, 2026
Merged

tannevaled merged 4 commits into
mainfrom
feat/agent-arraymut

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI-Ruby-4.0.6 conformance for the Array mutator/index cluster.

Fixes

  • Array#[]= — single-index assignment at or beyond the end now auto-grows, nil-padding the gap (was: IndexError), matching MRI (a=[]; a[1]=14 #=> [nil, 14]). The (start,length) and Range forms pad beyond the end; a Range subclass and non-Integer (#to_int) endpoints are accepted; a multi-element rhs is coerced via #to_ary and an Array subclass is spliced element-by-element; the return value is always the raw rhs. Negative-index / negative-start / RangeError diagnostics now carry MRI's exact messages (index -5 too small for array; minimum: -3).
  • Array#values_at — accepts Range arguments (subclasses, #to_int endpoints, beginless/endless, exclusive), padding out-of-bounds positions with nil and raising RangeError only for a begin still negative after normalisation.
  • Array#insert / #rotate / #rotate! — coerce the index/count via #to_int (so a Float or #to_int object works).
  • Array#dig — digs into the extracted value by calling its own #dig (an overridden/mocked #dig is honoured), raises ArgumentError with no keys, and raises TypeError for a nested value with no #dig.
  • Array#product — accepts a block (yields each combination, returns the receiver) and coerces arguments via #to_ary.
  • Array#flatten / #flatten! — coerce the depth argument via #to_int (a nil depth is unbounded).

ruby/spec deltas (core/array)

spec before after
element_set 50 pass / 2 fail / 12 err 64 / 0 / 0
values_at 2 / 0 / 8 err 10 / 0 / 0
insert 11 / 0 / 1 err 12 / 0 / 0
rotate 12 / 0 / 2 err 14 / 0 / 0
dig 6 / 2 fail 8 / 0
product 6 / 2 fail / 2 err 9 / 0 / 1 err
flatten 23 / 9 fail / 2 err 25 / 9 fail / 0 err

No regressions in the Array equality/membership specs (equal_value/eql/uniq/comparison unchanged); those functions were left untouched.

Remaining (out of scope of this cluster)

  • The 9 fill and several flatten failures are a spec-harness before :all instance-variable limitation (@never_passed resolves to nil) and a shared #to_ary-via-method_missing coercion gap (rb_check_funcall semantics) — not Array-mutator bugs.

Tests: internal/vm/array_mutator_residuals_test.go covers the new branches; the old TestArrayErrors case that assumed #[]= raised on an out-of-range index was repointed (MRI auto-grows).

🤖 Generated with Claude Code

tannevaled and others added 4 commits August 31, 2026 18:06
…nsert/rotate to_int

Array#[]= now auto-grows (nil-padding the gap) when a single index is at or
beyond the end, matching MRI, instead of raising IndexError. The negative
index and negative start/length errors now carry MRI's exact
"index N too small for array; minimum: -M" message. The Range form accepts
a Range subclass and coerces non-Integer endpoints via #to_int, and a begin
at/beyond the end pads rather than raising RangeError. Multi-element (range
and start,length) sets coerce a non-Array rhs through #to_ary and splice an
Array subclass element-by-element; the return value is always the raw rhs.

Array#values_at accepts Range arguments (including subclasses, #to_int
endpoints, beginless/endless), padding out-of-bounds positions with nil and
raising RangeError only for a begin still negative after normalisation.

Array#insert, #rotate and #rotate! coerce their index/count via #to_int
(so a Float or #to_int object works) rather than requiring an Integer.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…h #to_int

Array#dig now digs into the extracted value by calling its own #dig with the
remaining keys (so an overridden or mocked #dig is honoured), raises
ArgumentError with no keys, and raises TypeError ("X does not have #dig
method") for a nested value that has no #dig — matching MRI.

Array#product accepts a block (yielding each combination and returning the
receiver) and coerces its arguments via #to_ary.

Array#flatten/#flatten! coerce the depth argument via #to_int (a nil depth is
unbounded).

Adds array_mutator_residuals_test.go covering the new #[]=/#values_at/#insert/
#rotate/#dig/#product/#flatten branches, and repoints the old TestArrayErrors
case that assumed #[]= raised on an out-of-range index (MRI auto-grows).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…clamp branches

Refactors Array#dig to extract its first element inline and share the
continuation with Hash#dig via a new digRest helper, removing the unreachable
type-switch arms the earlier send-based recursion left behind. Adds tests for
the two Array#[]= Range negative-length clamps (endless begin-beyond-end and an
inverted range) and for Hash#dig's hit/miss/nested/arity paths, so every branch
of the changed functions is exercised.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit e706eae into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-arraymut branch August 31, 2026 22:09
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #535 (String methods: index/casecmp/chomp/gsub/scan/tr/... ~+110),
#536 (IO/StringIO reader+writer), #538 (Array#[]= auto-grow + mutators),
#539 (Range + Comparable), #540 (Struct + Data), #541 (Time). Measured
17983 passing on the pinned corpus, up from 17654.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant