Actions
Feature #16155
closedAdd an Array#intersection method
Status:
Closed
Assignee:
-
Target version:
-
Description
Array#union
and Array#difference
were added in Ruby 2.6 (see this bug), but an equivalent for &
(intersection) was not.
I'd like to propose Array#intersection
. This would essentially just be a more readable alias for Array#&
, in the same way that Array#|
and Array#-
have Array#union
and Array#difference
.
I think it'd make sense for Ruby to have a more readable name for this method :)
Current syntax:
[ 1, 1, 3, 5 ] & [ 3, 2, 1 ] #=> [ 1, 3 ]
[ 'a', 'b', 'b', 'z' ] & [ 'a', 'b', 'c' ] #=> [ 'a', 'b' ]
What I'd like to see added:
[ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ]) #=> [ 1, 3 ]
[ 'a', 'b', 'b', 'z' ].intersection([ 'a', 'b', 'c' ]) #=> [ 'a', 'b' ]
mame asks about intersection
in this comment on the union
/difference
bug, but as far as I can tell it was never addressed.
Set#intersection
already exists and is an alias for Set#&
, so there's precedent for such a method to exist.
Thanks for Ruby, I enjoy using it a lot! :)
Related links:
Actions
Like0
Like0Like0Like0Like0Like0Like0Like0