# Behavior of delete! method

**URL:** <https://www.ruby-forum.com/t/behavior-of-delete-method/142892>\
**Category:** Ruby\
**Created:** [July 12, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/behavior-of-delete-method/142892 "2008-07-12T20:54:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [July 12, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/behavior-of-delete-method/142892/1 "2008-07-12T20:54:18Z")

</div>

I am confused by the use of the delete! method as applied to a string.  
In IRB if I type

foo = “hello”  
bar = foo  
foo.delete!(“h”)

then both foo and bar become “ello”. How come bar isn’t independent of  
foo in this case?

If I then type

foo = foo + “oo”

then only foo becomes “ellooo” while bar remains “ello”

Why is bar independent of foo in the second case, but not the first?

Thanks,

Alex

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [July 12, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/behavior-of-delete-method/142892/2 "2008-07-12T21:15:08Z")

</div>

On Saturday 12 July 2008, Alex DeCaria wrote:

> If I then type
> 
> foo = foo + “oo”
> 
> then only foo becomes “ellooo” while bar remains “ello”
> 
> Why is bar independent of foo in the second case, but not the first?
> 
> Thanks,
> 
> Alex

The line

bar = foo

doesn’t create a copy of foo but simply stores in bar the same object  
which is  
already stored in foo (if you look at the object\_id of foo and bar,  
you’ll see  
they’re equal). Since delete! modifies its receiver in place, you see  
the  
changes also when you access the object using bar, not only when using  
foo.

The String#+ operator, instead, doesn’t modify its receiver in place,  
but  
returns a new string, which contains the characters of the first one  
concatenated with those of the second one. So, after

foo = foo + “oo”

the object (string) contained in foo is different from the one contained  
in  
bar and subsequent changes to the object stored in foo don’t affect bar  
at  
all.

If you truly need that bar and foo contain two different strings with  
the same  
contents, you can use String#dup:

bar = foo.dup

This behavior is not exclusive of strings, but is a feature of the ruby  
language. This means that you may need to be careful when passing  
objects  
which allow in-place modifications.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [July 12, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/behavior-of-delete-method/142892/3 "2008-07-12T21:18:05Z")

</div>

2008/7/12 Alex DeCaria \<alex.decaria@…\>:

> I am confused by the use of the delete! method as applied to a string.  
> In IRB if I type
> 
> foo = “hello”

This creates the object “hello” of type String and foo points to it.

> bar = foo

Now bar points to the same “hello” object. (Type ‘bar.object\_id’ and  
‘foo.object\_id’ to see they have the same id).

> foo.delete!(“h”)

Now, the “hello” object is modified, so foo and bar point to the  
modified “ello” object.

> then both foo and bar become “ello”. How come bar isn’t independent of  
> foo in this case?

For making bar independent of foo, you should do ‘bar = foo.clone’  
(check the object\_id and you’ll see the change).

> If I then type
> 
> foo = foo + “oo”
> 
> then only foo becomes “ellooo” while bar remains “ello”
> 
> Why is bar independent of foo in the second case, but not the first?

Because now, you’re making foo point to a new object and not changing  
the object.

In general, the methods whose name ends in ! do change the object.

Regards,

Serabe

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [July 12, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/behavior-of-delete-method/142892/4 "2008-07-12T21:22:29Z")

</div>

Thanks to both responders. That does clear it up!

Alex
