Skip to content

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

Merged
jeremyevans merged 3 commits into
ruby:masterfrom
jeremyevans:array-methods-return-array
Nov 3, 2020
Merged

Make Array methods return Array instances instead of subclass instances#3690
jeremyevans merged 3 commits into
ruby:masterfrom
jeremyevans:array-methods-return-array

Conversation

@jeremyevans

Copy link
Copy Markdown
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

@marcandre marcandre left a comment

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.

Sorry for the delay in reviewing 😅

Comment thread array.c Outdated
Comment thread test/ruby/test_array.rb
@ioquatix

ioquatix commented Nov 3, 2020

Copy link
Copy Markdown
Member

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

@jeremyevans

Copy link
Copy Markdown
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
Copy Markdown
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.

@marcandre marcandre left a comment

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.

LGTM 👍

@jeremyevans

Copy link
Copy Markdown
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