Skip to content

Fix Enumerable#sole when element is a tuple#55808

Merged
byroot merged 1 commit intorails:mainfrom
olivier-thatch:fix-enum-sole
Sep 30, 2025
Merged

Fix Enumerable#sole when element is a tuple#55808
byroot merged 1 commit intorails:mainfrom
olivier-thatch:fix-enum-sole

Conversation

@olivier-thatch
Copy link
Copy Markdown
Contributor

Motivation / Background

This Pull Request has been created because #54341 introduced a behavior change for Enumerable#sole when the sole element is a tuple.

Previously, Enumerable#sole would return the full tuple (same as Enumerable#first). #54341 changed the behavior so that Enumerable#sole only returns the first element of the tuple.

Fixes #55807.

Detail

This Pull Request changes Enumerable#sole to return the full tuple, i.e. the same value as Enumerable#first.

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Comment thread activesupport/lib/active_support/core_ext/enumerable.rb
@matthewd
Copy link
Copy Markdown
Member

I imagine this would now break [[1]].sole too 😕

@byroot
Copy link
Copy Markdown
Member

byroot commented Sep 30, 2025

I imagine this would now break [[1]].sole too 😕

Nope.

Comment on lines +410 to +422
class KeywordYielder
include Enumerable

def each
yield 1, two: 3
end
end

def test_sole_keyword_arguments
yielder = KeywordYielder.new
assert_equal [1, {two: 3}], yielder.sole
assert_equal yielder.first, yielder.sole
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed an extra test to make sure *arguments do also collect keyword args.

@byroot byroot merged commit c72f671 into rails:main Sep 30, 2025
2 of 3 checks passed
byroot added a commit that referenced this pull request Sep 30, 2025
Fix `Enumerable#sole` when element is a tuple
byroot added a commit that referenced this pull request Sep 30, 2025
Fix `Enumerable#sole` when element is a tuple
@byroot
Copy link
Copy Markdown
Member

byroot commented Sep 30, 2025

Thanks for the fix, backported to 8-0-stable, and 7-2-stable.

@olivier-thatch olivier-thatch deleted the fix-enum-sole branch September 30, 2025 18:10
@olivier-thatch
Copy link
Copy Markdown
Contributor Author

Thanks for the quick merge!

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.

Enumerable#sole returns only the first element of tuples

3 participants