# Ruby string question

**URL:** <https://www.ruby-forum.com/t/ruby-string-question/159112>\
**Category:** Ruby\
**Created:** [January 31, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112 "2009-01-31T19:40:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 31, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/1 "2009-01-31T19:40:06Z")

</div>

Is it possible to modify an existing ruby string without creating a new  
one ?

If for example I have the following code :

x = “first string”  
x = “second string”

between asigning values , the object\_id() is different . Is there a  
possibility to modify the first string so that after the modifications  
the object\_id will be the same ?

I would like to know how I could get to an empty string from “first  
string”. I would like to know if a clear method exists,or something  
alike. I’m using ruby 1.8.7 .

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 31, 2009, 7:51pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/2 "2009-01-31T19:51:39Z")

</div>

On Sat, Jan 31, 2009 at 7:40 PM, Tsunami S.er [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is it possible to modify an existing ruby string without creating a new  
> one ?  
> yes
> 
> If for example I have the following code :
> 
> x = “first string”  
> x “second string”  
> x.clear \<\< “second string”  
> x.sub! /first/, “second”  
> x[0…4] = “second”

> I would like to know how I could get to an empty string from “first  
> string”.  
> I guess my first example answers this.  
> HTH  
> Robert

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 31, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/3 "2009-01-31T19:57:51Z")

</div>

irb(main):005:0\> x = “string”  
=\> “string”  
irb(main):006:0\> x.clear  
NoMethodError: undefined method `clear’ for “string”:String  
from (irb):6  
from :0

Perhaps you’re using a different version of ruby .

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 31, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/4 "2009-01-31T21:02:38Z")

</div>

On Sat, Jan 31, 2009 at 7:40 PM, Tsunami S.er [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the object\_id will be the same ?
> 
> > a = “first string”  
> > =\> “first string”  
> > a.replace “second string”  
> > =\> “second string”  
> > a  
> > =\> “second string”  
> > a.object\_id  
> > =\> 340702860  
> > a.replace “third string”  
> > =\> “third string”  
> > a.object\_id  
> > =\> 340702860

> I would like to know how I could get to an empty string from “first  
> string”. I would like to know if a clear method exists,or something  
> alike. I’m using ruby 1.8.7 .

a.replace “”

Jesus.

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 31, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/5 "2009-01-31T20:20:57Z")

</div>

On Sat, Jan 31, 2009 at 7:57 PM, Tsunami S.er [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> irb(main):005:0\> x = “string”  
> =\> “string”  
> irb(main):006:0\> x.clear  
> NoMethodError: undefined method `clear’ for “string”:String  
> from (irb):6  
> from :0
> 
> Perhaps you’re using a different version of ruby .  
> Yup, I thought this was back-ported to 1.8.7 my error.

R

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [February 2, 2009, 7:02am UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/6 "2009-02-02T07:02:05Z")

</div>

x.delete!(x) \<\< “new value”

Before and after, the object id will be identical.

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 31, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/7 "2009-01-31T21:12:26Z")

</div>

Just what I needed.

Thanks !

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [February 2, 2009, 8:11am UTC](https://www.ruby-forum.com/t/ruby-string-question/159112/8 "2009-02-02T08:11:41Z")

</div>

> Is it possible to modify an existing ruby string without creating a new  
> one ?  
> […]  
> between asigning values , the object\_id() is different

I could be totally wrong but IMHO you’re asking for two different  
things.

As soon as you have something like “second string” in your code you  
will create a second string. You can assign this to the variable (the  
variable’s value will have a different object\_id) or replace its  
content with that string (the variables value will have same object\_id  
as before but in between you created a second string).

3.times do  
puts “---------”  
x = “one”.tap {|s| puts “one #{s.object\_id}”}  
p x, x.object\_id  
x.replace(“two”.tap {|s| puts “two #{s.object\_id}”})  
p x, x.object\_id  
end
