# Array#delete return value

**URL:** <https://rubytalk.org/t/array-delete-return-value/12920>\
**Category:** ruby-talk\
**Created:** [5 August 2004 20:39 UTC](https://rubytalk.org/t/array-delete-return-value/12920 "2004-08-05T20:39:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_Marcais1](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Guillaume\_Marcais1](https://rubytalk.org/u/Guillaume_Marcais1)\
**Post date:** [5 August 2004 20:39 UTC](https://rubytalk.org/t/array-delete-return-value/12920/1 "2004-08-05T20:39:10Z")

</div>

According to the following contrived example (and the documentation),  
Array#delete returns the object passed as argument if a match is found  
and deleted:

[gus@comp tmp]$ cat delete.rb  
Toto = Struct.new("Toto", :a, :b)  
class Toto; def ==(t); a == t.a; end; end  
t1 = Toto.new(1, 2)  
t2 = Toto.new(1, 3)  
p t1 == t2  
a = [t1]  
p [t1.id](http://t1.id)  
p [t2.id](http://t2.id)  
p a.delete(t2).id  
[gus@comp tmp]$ ruby delete.rb  
true  
578438406  
578438356  
578438356

It seems to me it would make more sense to return the object actually  
removed from the array (t1 in the example instead of t2).

The behavior I would expect is as follow:

[gus@comp tmp]$ cat delete2.rb  
class Array  
&nbsp;&nbsp;def delete2(o)  
&nbsp;&nbsp;&nbsp;&nbsp;i = index(o)  
&nbsp;&nbsp;&nbsp;&nbsp;return nil if i.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;delete\_at(i)  
&nbsp;&nbsp;end  
end

Toto = Struct.new("Toto", :a, :b)  
class Toto; def ==(t); a == t.a; end; end  
t1 = Toto.new(1, 2)  
t2 = Toto.new(1, 3)  
p t1 == t2  
a = [t1]  
p [t1.id](http://t1.id)  
p [t2.id](http://t2.id)  
p a.delete2(t2).id  
[gus@comp tmp]$ ruby delete2.rb  
true  
663778146  
663778096  
663778146

Any thoughts on the matter.  
Guillaume.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [6 August 2004 05:08 UTC](https://rubytalk.org/t/array-delete-return-value/12920/2 "2004-08-06T05:08:39Z")

</div>

Hi,

> **···**
>
> In message "Array#delete return value" on 04/08/06, Guillaume Marcais \<guslist@free.fr\> writes:
> 
> > According to the following contrived example (and the documentation),  
> > Array#delete returns the object passed as argument if a match is found  
> > and deleted:
> 
> > It seems to me it would make more sense to return the object actually  
> > removed from the array (t1 in the example instead of t2).
> 
> But Array#delete method deletes ALL elements that equal to the  
> argument. Do you want it to return an array of deleted items?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
