Skip to content

Fix Enumerable#in_order_of dropping nil elements when filter: false - #57553

Merged
fatkodima merged 1 commit into
rails:mainfrom
hammadxcm:fix-in-order-of-filter-false-drops-nil
Jun 3, 2026
Merged

fatkodima merged 1 commit into
rails:mainfrom
hammadxcm:fix-in-order-of-filter-false-drops-nil

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Summary

Enumerable#in_order_of with filter: false is documented to keep elements that aren't named in the series:

If the Enumerable has additional elements that aren't named in the series, these are not included in the result, unless the filter option is set to false.

But a stray compact (copied from the filter: true branch) silently removed nil elements:

[3, nil, 1, 2].in_order_of(:itself, [1, 2, 3], filter: false)
# => [1, 2, 3]        (before — nil dropped)
# => [1, 2, 3, nil]   (after  — nil preserved)

Why the compact is wrong here

The filter: true branch needs compact because group_by(&key).values_at(*series) produces nil for series entries with no matching element. The filter: false branch uses sort_by, which never introduces nil, so the compact only stripped genuine nil elements from the collection — contradicting the documented contract.

Changes

  • Remove the compact from the filter: false branch.
  • Add a regression test.
  • CHANGELOG entry.

All existing activesupport enumerable_test.rb tests pass.

Enumerable#in_order_of with filter: false is documented to keep
elements that aren't named in the series, but a stray compact (copied
from the filter: true branch) silently removed nil elements.

The filter: true branch needs compact because values_at produces nil
for series entries with no matching element; the filter: false branch
uses sort_by, which never introduces nil, so the compact only stripped
genuine nil elements from the collection.

Remove it so nil elements are preserved and sorted to the end like any
other unnamed element.
@fatkodima
fatkodima force-pushed the fix-in-order-of-filter-false-drops-nil branch from 2d2cf1c to 30d0cdf Compare June 3, 2026 08:52
@fatkodima
fatkodima merged commit 4adf85f into rails:main Jun 3, 2026
2 checks passed
fatkodima added a commit that referenced this pull request Jun 3, 2026
…-drops-nil

Fix `Enumerable#in_order_of` dropping nil elements when `filter: false`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants