Skip to content

Enumerable: advance MRI 4.0.6 conformance (aliases, sizes, min/max/reduce/find, cycle/zip/sum/lazy) - #532

Merged
tannevaled merged 6 commits into
mainfrom
feat/agent-enumerable
Aug 31, 2026
Merged

tannevaled merged 6 commits into
mainfrom
feat/agent-enumerable

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances core/enumerable/*_spec.rb conformance toward MRI Ruby 4.0.6. Enumerable total across the corpus went from ~pass=485/fail=48/error=25 to pass=515 / fail=8 / error=4, with these full-spec files now passing completely: collect, detect, filter, find_all, reduce, collect_concat, flat_map, find, find_index, take_while, drop_while, each_with_index, each_with_object, each_entry, entries, member, to_a, to_h, to_set, tally, min, max, minmax, min_by, max_by, minmax_by, group_by, partition, filter_map, select, reject, map (all non-Hash/VM parts), sum, cycle, zip, lazy, reverse_each.

Changes (all in internal/vm/prelude.rb Enumerable, plus one line in internal/vm/lazy.go)

  • Genuine aliases: collect/filter/detect/find_all/collect_concat/inject/entries/member? are now real aliases of their targets, so Enumerable.instance_method(:collect) == instance_method(:map) etc., matching MRI.
  • Enumerator #size: origin-size methods (map, select, reject, flat_map, min_by, max_by, sort_by, minmax_by, group_by, partition, each_with_index, each_with_object, filter_map) report the receiver size when it responds to #size and nil otherwise; unknown-size methods (find, find_index, take_while, drop_while) report nil.
  • min/max/minmax: compare with the block when given, else <=>; raise ArgumentError on a nil comparison; gather multi-value yields as arrays.
  • reduce/inject: accept a String / #to_str operator, decide a lone argument as init vs operator by block presence, and raise the right TypeError/ArgumentError.
  • find/detect: accept an ifnone argument.
  • Argument forwarding to #each for to_a, to_h, each_with_index, each_entry.
  • to_h coerces pairs with Array.try_convert (#to_ary); to_set accepts the set class; tally converts a non-Hash argument via #to_hash.
  • map/find_index/take_while forward the raw yield so block arity governs.
  • flat_map uses Array.try_convert per element; zip converts via #to_ary/#to_enum(:each) with a TypeError fallback; sum uses Kahan-Babuska-Neumaier compensation for floats; cycle coerces n via #to_int, no-ops for non-positive n without touching #each, and iterates lazily on the first pass.
  • Enumerable#lazy exposed on the module so any #each-defining class gets it.

The frozen prelude (prelude_frozen_gen.go) was regenerated via go generate.

Known residuals (out of scope for the Enumerable builtins)

  • grep/grep_v $~ preservation needs frame-local $~ and a working $~= (VM-level).
  • A few map cases depend on Hash#each yielding two args and on super/block-arity mechanics (VM/Hash).
  • sort_by's one failure is a spec_helper mock limitation (and_yield chain yields only the last value).
  • uniq/sort/inject mutation cases route to native Array methods.

Verification

  • go test ./internal/vm/ green; go test ./... green; GOOS=js GOARCH=wasm build OK; gofmt -l clean on changed Go files.
  • MERGED coverage (-coverpkg ./internal/...) = 100.0%.

🤖 Generated with Claude Code

tannevaled and others added 6 commits August 31, 2026 15:37
…mily

- collect/filter/detect/find_all/collect_concat/inject are now genuine
  aliases (alias, not wrapper defs), so Enumerable.instance_method(:collect)
  == instance_method(:map) etc., matching MRI 4.0.6.
- flat_map now uses Array.try_convert on each yielded value: concatenates a
  #to_ary result, appends when it returns nil, raises TypeError otherwise.
- enum_for size functions match MRI: origin-size methods (map, select,
  reject, flat_map, min_by, max_by, sort_by, minmax_by, group_by, partition,
  each_with_index, each_with_object, filter_map) return the receiver size
  when it responds to #size and nil otherwise; unknown-size methods (find,
  find_index, take_while, drop_while) return nil.
- Regenerated prelude_frozen_gen.go.

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

- min/max/minmax now compare with the block when given, else with <=>, and
  raise ArgumentError on a nil comparison (incomparable elements or a block
  returning nil), matching MRI. Multi-value #each yields are compared as
  gathered arrays; min(n)/max(n) sort with the block.
- reduce/inject accept a String (or #to_str) operator, decide a lone argument
  as init vs operator by block presence, raise TypeError for a non-symbol/
  non-string operator, and ArgumentError for zero args without a block or more
  than two args.
- find/detect accept an ifnone argument, calling it when nothing matches
  (raising NoMethodError if it does not respond to #call) and threading it
  through the no-block enumerator.
- Regenerated prelude_frozen_gen.go.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…y, arity

- entries is an alias of to_a and member? an alias of include? (genuine
  aliases, matching MRI's instance_method identity).
- to_a, to_h, each_with_index, and each_entry forward their arguments to
  #each, as MRI does.
- to_h coerces each pair with Array.try_convert (#to_ary), matching MRI's
  element-type and array-length errors.
- to_set accepts the set class as an optional first argument (plus extra
  args), instantiating it with the elements.
- tally converts a non-Hash argument with #to_hash before tallying into it.
- map, find_index, and take_while forward the raw #each yield to the block so
  block arity governs multi-value yields (a single-parameter block takes the
  first value; a splat gathers), matching MRI.
- Regenerated prelude_frozen_gen.go.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- zip converts a non-Array argument with #to_ary, else #to_enum(:each), and
  raises TypeError (must respond to :each) when it responds to neither,
  matching MRI.
- sum switches to Kahan-Babuska-Neumaier compensated summation once a Float is
  involved, so a long run of floats sums without drift.
- cycle coerces n through #to_int (TypeError on failure), does nothing without
  touching #each for a non-positive n, and drives #each directly on the first
  pass (buffering for later cycles) so a breaking block yields only as needed
  and #each is called at most once.
- Regenerated prelude_frozen_gen.go.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Any class that mixes in Enumerable and defines #each now answers #lazy,
returning an Enumerator::Lazy driven through #each (lazySource's default
path already materialises an arbitrary receiver via #each). Previously #lazy
was defined only on Array, Range, Hash and Enumerator.

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

New vm_test cases in enumerable_residuals_test.go covering the genuine
aliases (identical UnboundMethods), origin- vs unknown-size enumerators,
min/max/minmax comparator + ArgumentError, reduce/inject operator forms,
find ifnone, argument forwarding, to_h #to_ary coercion, flat_map, lazy on a
bare Enumerable, and the reduce/find/cycle/zip error branches.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 160a51c into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-enumerable branch August 31, 2026 15:00
tannevaled added a commit that referenced this pull request Aug 31, 2026
Locks in the gains from #530 (Encoding compatibility: Converter.new,
concat negotiation, multibyte codepoints, ascii_only?), #531 (language
def/method/super/keyword-argument), #532 (Enumerable residuals) and #533
(Array/Hash value equality dispatching #==/#eql?/#hash). Measured 17654
passing on the pinned corpus, up from 17470.

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