# String.insert newbie question

**URL:** <https://www.ruby-forum.com/t/string-insert-newbie-question/83543>\
**Category:** Ruby\
**Created:** [January 27, 2007, 2:00am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543 "2007-01-27T02:00:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [January 27, 2007, 2:00am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/1 "2007-01-27T02:00:36Z")

</div>

Hello,

I’ve only been checking out Ruby for a couple of days, but today I came  
across this problem.

If I do this:

#!/usr/bin/env ruby -w

string1 = “abc”  
string2 = string1

string2.insert(-1, “def”)

puts string1  
puts string2

Then the result is:

abcdef  
abcdef

How come string1 gets changed?!?

Thanks,

Marco

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [January 27, 2007, 2:05am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/2 "2007-01-27T02:05:36Z")

</div>

On 1/26/07, Marco G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> string1 = “abc”  
> abcdef
> 
> check it out in irb.

irb(main):001:0\> string1 = “abc”  
=\> “abc”  
irb(main):002:0\> string2 = string1  
=\> “abc”  
irb(main):003:0\> string2.object\_id  
=\> 23129580  
irb(main):004:0\> string1.object\_id  
=\> 23129580

Hope that helps.

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [January 27, 2007, 2:30am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/3 "2007-01-27T02:30:37Z")

</div>

On 1/27/07, Marco G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jason M. wrote:
> 
> > > check it out in irb.
> 
> > Hope that helps.
> 
> Thanks for clearing that up, didn’t know about object\_id. Is this  
> behaviour common in programming languages? Seems weird to me.

It’s not weird if you consider :

customer0 = Customer.new(“John”, “Doe”)  
customer1 = customer0  
customer0.first\_name = “Paul”  
puts customer1.first\_name

customer0 and customer1 points to the same instance.

Java strings are misleading.

Best regards,

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [January 27, 2007, 2:22am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/4 "2007-01-27T02:22:40Z")

</div>

Jason M. wrote:

> > check it out in irb.

> Hope that helps.

Thanks for clearing that up, didn’t know about object\_id. Is this  
behaviour common in programming languages? Seems weird to me.

Then again I’m very new to all of this.

This means I could do

string1 = “abc”  
string2 = string1  
string3 = string2  
string4 = string3

and they all will change if I change string 4. What can I do to keep  
string1 stay the same even if I change string 4?

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [January 27, 2007, 2:40am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/5 "2007-01-27T02:40:06Z")

</div>

John M. wrote:

> customer0 = Customer.new(“John”, “Doe”)  
> customer1 = customer0  
> customer0.first\_name = “Paul”  
> puts customer1.first\_name
> 
> customer0 and customer1 points to the same instance.

Yes, that makes sense. I’ve even found a much simpler solution to my  
original problem that makes the question irrelevant.

Thanks, everyone!

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [January 27, 2007, 9:50am UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/6 "2007-01-27T09:50:08Z")

</div>

Hi!

On Jan 27, 2007, at 04:40, Marco G. wrote:

> Yes, that makes sense. I’ve even found a much simpler solution to my  
> original problem that makes the question irrelevant.
> 
> Thanks, everyone!

I would like to notice, that if you ever need two separate objects, you  
can always use a dup method, e.g:

string2 = string1.dup # Will have two different string instances

Your sincerely,  
Damian/Three-eyed Fish

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [September 29, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/string-insert-newbie-question/83543/7 "2009-09-29T17:26:16Z")

</div>

Thanks very, very much, I was mired in a problem with this and you’ve  
rescued me.  
Bill M.  
Eloqua

Damian T. wrote:

> Hi!
> 
> I would like to notice, that if you ever need two separate objects, you  
> can always use a dup method, e.g:
> 
> string2 = string1.dup # Will have two different string instances
> 
> Your sincerely,  
> Damian/Three-eyed Fish
