In ruby, Array#delete(obj) will search and remove the specified object from the array. However, may be I'm missing something here but I found the returning value --- the obj itself --- is quite strange and a even a little bit useless.
My humble opinion is that in consistent with methods like sort/sort! and map/map! there should be two methods, e.g. delete/delete!, where
ary.delete(obj) -> new array, with obj removed
ary.delete!(obj) -> ary (after removing obj from ary)
For several reasons, first being that current delete is non-pure, and it should warn the programmer about that just like many other methods in Array (in fact the entire delete_??? family has this issue, they are quite dangerous methods!), second being that returning the obj is much less chainable than returning the new array, for example, if delete were like the above one I described, then I can do multiple deletions in one statement, or I can do something else after deletion:
ary = [1,2,2,2,3,3,3,4]
ary.delete(2).delete(3) #=> [1,4], equivalent to "ary - [2,3]"
ary.delete(2).map{|x|x**2"} #=> [1,9,9,9,16]
which is elegant and easy to read.
So I guess my question is: is this a deliberate design out of some reason, or is it just a heritage of the language?
a=[1,2,3]; a.delete_if { |e| e==2 } => [1,3]; a=>[1,3]or[1,2,3].delete_if { |e| e==2 }.map { |e| e**2 } => [1, 9]. On the other hand,if arr.delete(3)...is often quite handy, and rather nicer thansz=arr.size; if sz-arr.delete.size > 0...(i.e., ifdeletereturned the resulting array) . I don't think you can evaluate a method's spec in isolation of those of other methods. The Ruby language monks are pretty sharp and usually have a good reason for what they do.