[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

behavior of delete! method

Alex DeCaria

7/12/2008 6:50:00 PM

I am confused by the use of the delete! method as applied to a string.
In IRB if I type

foo = "hello"
bar = foo
foo.delete!("h")

then both foo and bar become "ello". How come bar isn't independent of
foo in this case?

If I then type

foo = foo + "oo"

then only foo becomes "ellooo" while bar remains "ello"

Why is bar independent of foo in the second case, but not the first?

Thanks,

Alex
--
Posted via http://www.ruby-....

3 Answers

Stefano Crocco

7/12/2008 7:10:00 PM

0

On Saturday 12 July 2008, Alex DeCaria wrote:
> I am confused by the use of the delete! method as applied to a string.
> In IRB if I type
>
> foo = "hello"
> bar = foo
> foo.delete!("h")
>
> then both foo and bar become "ello". How come bar isn't independent of
> foo in this case?
>
> If I then type
>
> foo = foo + "oo"
>
> then only foo becomes "ellooo" while bar remains "ello"
>
> Why is bar independent of foo in the second case, but not the first?
>
> Thanks,
>
> Alex

The line

bar = foo

doesn't create a copy of foo but simply stores in bar the same object which is
already stored in foo (if you look at the object_id of foo and bar, you'll see
they're equal). Since delete! modifies its receiver in place, you see the
changes also when you access the object using bar, not only when using foo.

The String#+ operator, instead, doesn't modify its receiver in place, but
returns a new string, which contains the characters of the first one
concatenated with those of the second one. So, after

foo = foo + "oo"

the object (string) contained in foo is different from the one contained in
bar and subsequent changes to the object stored in foo don't affect bar at
all.

If you truly need that bar and foo contain two different strings with the same
contents, you can use String#dup:

bar = foo.dup

This behavior is not exclusive of strings, but is a feature of the ruby
language. This means that you may need to be careful when passing objects
which allow in-place modifications.

I hope this helps

Stefano


Serabe

7/12/2008 7:12:00 PM

0

2008/7/12 Alex DeCaria <alex.decaria@...>:
> I am confused by the use of the delete! method as applied to a string.
> In IRB if I type
>
> foo = "hello"

This creates the object "hello" of type String and foo points to it.

> bar = foo

Now bar points to the same "hello" object. (Type 'bar.object_id' and
'foo.object_id' to see they have the same id).

> foo.delete!("h")

Now, the "hello" object is modified, so foo and bar point to the
modified "ello" object.

> then both foo and bar become "ello". How come bar isn't independent of
> foo in this case?

For making bar independent of foo, you should do 'bar = foo.clone'
(check the object_id and you'll see the change).

> If I then type
>
> foo = foo + "oo"
>
> then only foo becomes "ellooo" while bar remains "ello"
>
> Why is bar independent of foo in the second case, but not the first?

Because now, you're making foo point to a new object and not changing
the object.

In general, the methods whose name ends in ! do change the object.

Regards,

Serabe

> Thanks,
>
> Alex
> --
> Posted via http://www.ruby-....
>
>



--
http://www....

Alex DeCaria

7/12/2008 7:18:00 PM

0

Thanks to both responders. That does clear it up!

Alex
--
Posted via http://www.ruby-....