# Question regarding sub!()

**URL:** <https://www.ruby-forum.com/t/question-regarding-sub/150177>\
**Category:** Ruby\
**Created:** [October 5, 2008, 12:18pm UTC](https://www.ruby-forum.com/t/question-regarding-sub/150177 "2008-10-05T12:18:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [October 5, 2008, 12:18pm UTC](https://www.ruby-forum.com/t/question-regarding-sub/150177/1 "2008-10-05T12:18:07Z")

</div>

Hi,

Please excuse the newbie question, but I am confused by something I see  
with  
sub!() (this seems to be true with other “!” operations as well):

irb(main):001:0\> string1 = “hello”  
=\> “hello”  
irb(main):002:0\> string2 = string1  
=\> “hello”  
irb(main):003:0\> string2.sub!(/e/,“a”)  
=\> “hallo”  
irb(main):004:0\> string2  
=\> “hallo”  
irb(main):005:0\> string1  
=\> “hallo”  
irb(main):006:0\>

Why is string1 also changed when sub!() is being performed on string2 ?  
Shouldn’t sub!() only be acting on the string it is being called on?

Thanks,  
Martin

---

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [October 5, 2008, 12:38pm UTC](https://www.ruby-forum.com/t/question-regarding-sub/150177/2 "2008-10-05T12:38:55Z")

</div>

when writing :

string2 = string1

both your variables reference the same address , so , modifications to  
any of them will cause modification to the other .

If you write :

string2 = string1.clone

then you will modify just string2 , when writing sub!

---

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [October 5, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/question-regarding-sub/150177/3 "2008-10-05T13:28:30Z")

</div>

On Sun, Oct 5, 2008 at 6:37 AM, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## then you will modify just string2 , when writing sub!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

But, why then, the following is not true???  
n1 = 100  
n2 = n2

To this point the are the same, including the object\_id.  
But when I assign:  
n2 = 200  
The n1 does not change and the object\_id of n2 change.  
Please see the irb session below.

irb  
irb(main):001:0\> n1 = 100  
=\> 100  
irb(main):002:0\> n2 = n1  
=\> 100  
irb(main):003:0\> p n1  
100  
=\> nil  
irb(main):004:0\> p n2  
100  
=\> nil  
irb(main):005:0\> p n1.object\_id  
201  
=\> nil  
irb(main):006:0\> p n2\_object\_id  
NameError: undefined local variable or method `n2\_object\_id’ for  
main:Object  
from (irb):6  
from :0  
irb(main):007:0\> p n2.object\_id  
201  
=\> nil  
irb(main):008:0\> n2 = 200  
=\> 200  
irb(main):009:0\> p n1  
100  
=\> nil  
irb(main):010:0\> p n2  
200  
=\> nil  
irb(main):011:0\> p n1.object\_id  
201  
=\> nil  
irb(main):013:0\> p n2.object\_id  
401  
=\> nil  
irb(main):014:0\>

Thank you

Ruby S.

---

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [October 5, 2008, 1:34pm UTC](https://www.ruby-forum.com/t/question-regarding-sub/150177/4 "2008-10-05T13:34:21Z")

</div>

Hi –

On Sun, 5 Oct 2008, Ruby S. wrote:

> > 
> 
> To this point the are the same, including the object\_id.  
> But when I assign:  
> n2 = 200  
> The n1 does not change and the object\_id of n2 change.

When you assign to a variable, you break any previous binding the  
variable had.

a = “string”  
b = a #1  
b = 100 #2

At #1, the identifiers ‘a’ and ‘b’ refer to the same object. At #2,  
however, ‘b’ has been bound to another object. Nothing you do to the  
string in a will affect the object in b (100).

David
