# New, but this seems odd

**URL:** <https://www.ruby-forum.com/t/new-but-this-seems-odd/191205>\
**Category:** Ruby\
**Created:** [July 7, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205 "2010-07-07T15:02:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 7, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205/1 "2010-07-07T15:02:47Z")

</div>

Ok, granted that I’m pretty new to ruby, but this seems odd to me:

irb(main):001:0\> x = [1,2,3]  
=\> [1, 2, 3]  
irb(main):002:0\> x.delete 2  
=\> 2  
irb(main):003:0\> x  
=\> [1, 3]

With this behavior, wouldn’t “delete!” be a more appropriate name?

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 7, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205/2 "2010-07-07T15:26:13Z")

</div>

On Jul 7, 2010, at 9:01 AM, Andrew W. wrote:

> Ok, granted that I’m pretty new to ruby, but this seems odd to me:
> 
> irb(main):001:0\> x = [1,2,3]  
> =\> [1, 2, 3]  
> irb(main):002:0\> x.delete 2  
> =\> 2  
> irb(main):003:0\> x  
> =\> [1, 3]
> 
> With this behavior, wouldn’t “delete!” be a more appropriate name?

Read  
[http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist](http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist)

Is there some kind of interpretation of “delete” that doesn’t imply  
the receiving object will be changed?

Compare Hash#update and Hash#merge, for example. Hash#update changes  
the receiving hash, which Hash#merge creates a new hash with extra/  
overwritten keys from the argument hash. It shouldn’t be too  
surprising that Hash#merge! is an alias of Hash#update.

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 7, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205/3 "2010-07-07T15:25:03Z")

</div>

On Wed, Jul 07, 2010 at 10:01:35PM +0900, Andrew W. wrote:

> Ok, granted that I’m pretty new to ruby, but this seems odd to me:
> 
> irb(main):001:0\> x = [1,2,3]  
> =\> [1, 2, 3]  
> irb(main):002:0\> x.delete 2  
> =\> 2  
> irb(main):003:0\> x  
> =\> [1, 3]
> 
> With this behavior, wouldn’t “delete!” be a more appropriate name?

This came up a few days ago:

removed\_email\_address@domain.invalid

David A. Black’s answer covers it nicely, I think.

Dan

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 7, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205/4 "2010-07-07T15:44:22Z")

</div>

On Wed, Jul 7, 2010 at 8:25 AM, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > =\> [1, 3]  
> > receiving object will be changed?

x = “123”  
x.delete ‘2’ # =\> “13”  
x # =\> “123”

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 7, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205/5 "2010-07-07T15:40:51Z")

</div>

That article is helpful, thanks. Somehow I had exactly that idea in my  
mind,  
that the ! implies side effects. I was expecting to get back a new array  
[1,  
3], and that there would be a delete! method defined with the below  
behavior. That seems more consistent to me than some vague description  
that  
! means “dangerous” for some value of “dangerous”. I guess I still have  
plenty to learn. Thanks for the link!

On Wed, Jul 7, 2010 at 9:25 AM, Rob B.

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 9, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/new-but-this-seems-odd/191205/6 "2010-07-09T16:50:49Z")

</div>

On Jul 7, 8:01 am, Andrew W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> With this behavior, wouldn’t “delete!” be a more appropriate name?

On Jul 7, 8:25 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there some kind of interpretation of “delete” that doesn’t imply  
> the receiving object will be changed?

After some reflection, I can certainly see how you would expect a  
bang! there… when working with sets, you often want to manipulate a  
set to form a different one.

guests = [‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’,  
‘me’]  
invitations\_to\_send = guests.delete(‘me’)

and in fact there are enumerable operators that work that way…

guest\_emails = [‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’,  
‘removed\_email\_address@domain.invalid’, ‘me’]  
guests = guest\_emails.map{|email| Person.find\_by\_email( email ) }

whereas

guest\_emails.map!{|email| Person.find\_by\_email( email ) }

So, yes, inconsistent. Sorry, not much that can be done now… 🙂

The way to do this would be …

invitations\_to\_send = guests - [‘me’]

or just overwrite delete for your app… 🙂

jw

* * *

Johnathon W.  
Web Application Consultant

[http://www.mustmodify.com/](http://www.mustmodify.com/)
