Skip to content

Make Array methods return Array instances instead of subclass instances #3690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2020

Conversation

jeremyevans
Copy link
Contributor

This changes the following methods to return Array instances instead
of subclass instances:

  • Array#flatten
  • Array#slice!
  • Array#slice/#[]
  • Array#uniq
  • Array#*

Fixes [Bug #6087]

@jeremyevans jeremyevans requested a review from marcandre October 27, 2020 21:59
Copy link
Member

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in reviewing 😅

@ioquatix
Copy link
Member

ioquatix commented Nov 3, 2020

Can't this break existing code that depends on this behaviour?

@jeremyevans
Copy link
Contributor Author

Can't this break existing code that depends on this behaviour?

Yes, though the breakage is probably minimal as subclassing Array is not that common, and most Array methods do not return subclass instances. Note that this behavior change was already approved by matz in bug 6087.

This changes the following methods to return Array instances instead
of subclass instances:

* Array#flatten
* Array#slice!
* Array#slice/#[]
* Array#uniq
* Array#*

Fixes [Bug ruby#6087]
Add assert_equal_instance in array tests for DRYer code.
@jeremyevans jeremyevans force-pushed the array-methods-return-array branch from 8551d60 to 9d0d3f8 Compare November 3, 2020 20:48
@jeremyevans
Copy link
Contributor Author

@marcandre I rebased this against current master and pushed another commit handling the issues you noted. Assuming it passes CI, I'll squash merge it.

Copy link
Member

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@jeremyevans
Copy link
Contributor Author

macOS failure is a timeout:

Multiple autoclose IO objects for a file descriptor in: TestNetHTTPS#test_certificate_verify_failure:  #<IO:<STDOUT>> #<IO:fd 1>(autoclose) #<IO:fd 1>(autoclose)
Multiple autoclose IO objects for a file descriptor in: TestNetHTTPS#test_certificate_verify_failure:  #<IO:<STDIN>> #<IO:fd 0>(autoclose) #<IO:fd 0>(autoclose)
.S..S..S.SMultiple autoclose IO objects for a file descriptor in: TestBundledCA#test_accessing_fastly:  #<IO:<STDOUT>> #<IO:fd 1>(autoclose) #<IO:fd 1>(autoclose)
Multiple autoclose IO objects for a file descriptor in: TestBundledCA#test_accessing_fastly:  #<IO:<STDIN>> #<IO:fd 0>(autoclose) #<IO:fd 0>(autoclose)
Skipping Gem::PackageTask tests.  rake not found.
Error: The action has timed out.

This appears to be unrelated to this PR, so I'll merge this. If it turns out this commit causes issues, we can revert.

@jeremyevans jeremyevans merged commit 2a294d4 into ruby:master Nov 3, 2020
mtasaka added a commit to mtasaka/coderay that referenced this pull request Feb 21, 2021
With ruby 3.0, especially with ruby/ruby#3690 ,
for subclass of Array, `flatten` method now returns the instance of Array,
not of the subclass.

To keep the object instance of the subclass, use `flatten!` instead.
junaruga pushed a commit to junaruga/coderay that referenced this pull request Mar 1, 2021
With ruby 3.0, especially with ruby/ruby#3690 ,
for subclass of Array, `flatten` method now returns the instance of Array,
not of the subclass.

To keep the object instance of the subclass, use `flatten!` instead.
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.

3 participants