We have Array#delete already, the only difference from your proposal (other than the receiver) being that it does not accept multiple arguments. I think a better proposal would be to let Array#delete accept multiple arguments.
In order to preserve the original behavior of returning the deleted elements, you would need to track them which would necessitate an additional array allocation so it would be no faster than Array#-=
.
We could return self
or nil
depending on whether or not an element is removed similar to Array#reject!
I believe that would still be faster.
My other concern with allowing multiple values in Array#delete
would be a possible performance degradation with existing code that only passes in 1 value. It's my experience through this PR that methods accepting multiple values are slower (however this may be due to a mistake on my part). I posted my code and comparison in this comment https://github.com/ruby/ruby/pull/5110#issuecomment-967276896.
It is a practice in Ruby for methods that destructively remove something from the receiver to return the removed value rather than the modified receiver.
That is very interesting. I never knew there was a pattern to the return results of bang methods. The differences have always been confusing to me, but I had never considered that they might behave differently if they are destructive. If this is the convention is it documented somewhere, could we document it somewhere? Where would be a good place to document this convention?
If the return value is the primary concern then we could follow in the pattern of Array#reject!
to return either self or nil.
I personally dislike the behavior as it means I cannot substitute bang methods that are chained because they may return different results. That means to write faster Ruby code, I must write worse ruby code. I do respect the precedent and I'm happy to change the return value.
I also do not think it is worth adding a new method just for 1.22x or so performance improvement.
That's much harder to workaround. Even the 1.22x number might be high.
I am new to Ruby c internals, so maybe it is possible that there are more optimizations that can be made with my code if it no longer has to allocate the extra array.
I appreciate your time and attention. We can close the ticket and pull request unless someone disagrees.