behavior of delete! method

A

Alex DeCaria

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
 
S

Stefano Crocco

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
 
S

Serabe

2008/7/12 Alex DeCaria said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top