From: Serabe Date: 2008-07-13T04:12:29+09:00 Subject: Re: behavior of delete! method 2008/7/12 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-forum.com/. > > -- http://www.serabe.com