# Copying strings

**URL:** <https://www.ruby-forum.com/t/copying-strings/82656>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/copying-strings/82656 "2007-01-19T16:51:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WoodHacker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ac8455/32.png) [@WoodHacker](https://www.ruby-forum.com/u/WoodHacker)\
**Post date:** [January 19, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/copying-strings/82656/1 "2007-01-19T16:51:07Z")

</div>

I can’t believe this hasn’t been answered a thousand times, but I can’t  
seem to find the answer in the group archive.

I am parsing a string into @text (i.e. abc.def) I want to save the  
part before the dot into @firstpart . For example:

```
           @text << "a"
           @text << "b"
           @text << "c" next char is "." so @firstpart = @text

```

then @text \<\< “.”  
@text \<\< “d”, etc

The problem is, this doesn’t work. @firstpart apparently is a  
pointer to @text and continues to receive the characters. Freezing  
@firstpart doesn’t work because then @text \<\< produces a freeze error.  
There doesn’t appear to be a “copy” method for string, so how do I do  
this?

Sometimes the obvious eludes us…

Bill

---

<div class="post-metadata">

**Author:** ![WoodHacker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ac8455/32.png) [@WoodHacker](https://www.ruby-forum.com/u/WoodHacker)\
**Post date:** [January 19, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/copying-strings/82656/2 "2007-01-19T16:55:08Z")

</div>

WoodHacker wrote:

> I can’t believe this hasn’t been answered a thousand times, but I can’t  
> seem to find the answer in the group archive.
> 
> I am parsing a string into @text (i.e. abc.def) I want to save the  
> part before the dot into @firstpart . For example:

why not

‘abc.def’.scan(/(.+)./)[0]

?

Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![WoodHacker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ac8455/32.png) [@WoodHacker](https://www.ruby-forum.com/u/WoodHacker)\
**Post date:** [January 19, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/copying-strings/82656/3 "2007-01-19T17:06:10Z")

</div>

WoodHacker wrote:

> ```
> @text << "d", etc
> 
> ```
> 
> The problem is, this doesn’t work. @firstpart apparently is a  
> pointer to @text and continues to receive the characters. Freezing  
> @firstpart doesn’t work because then @text \<\< produces a freeze error.  
> There doesn’t appear to be a “copy” method for string, so how do I do  
> this?
> 
> Sometimes the obvious eludes us…
> 
> Bill

Your code sample doesn’t run at all. Here’s one that does.

text = “”  
text \<\< “a”  
text \<\< “b”  
text \<\< “c”  
first\_part = text.dup  
text \<\< “.”  
text \<\< “d”

p first\_part

---

<div class="post-metadata">

**Author:** ![WoodHacker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ac8455/32.png) [@WoodHacker](https://www.ruby-forum.com/u/WoodHacker)\
**Post date:** [January 19, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/copying-strings/82656/4 "2007-01-19T19:46:38Z")

</div>

dup did the trick! Thanks a million

bill
