[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Copying strings

WoodHacker

1/19/2007 3:46:00 PM

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

3 Answers

Peter Szinek

1/19/2007 3:55:00 PM

0

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.rubyra...


William James

1/19/2007 4:01:00 PM

0

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:
>
> @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

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

WoodHacker

1/19/2007 6:41:00 PM

0

dup did the trick! Thanks a million

bill

William James wrote:
> 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:
> >
> > @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
>
> 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