Add an Array#intersection method
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
I think it'd make sense for Ruby to have a more readable name for this method :)
[ 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
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! :)