From: "Michael W. Ryder" <_mwryder@...> Date: 2007-07-21T05:15:00+09:00 Subject: Re: Is there a replacement for sub? Sebastian Hungerecker wrote: > Michael W. Ryder wrote: >> Martin DeMello wrote: > >>> a = "a b c d e f" >>> b = a >>> a = a.sub(" ", "") # => a is now "ab c d e f", b is still "a b c d e f" >> Wrong. > > Nope. > >> Unless you use b = a.dup b will be the same as a as it is >> pointing to the same object. > > Yes. > >> The result of the sub operation will >> change b as well as a. > > No, sub won't change the actual string - that's what sub! would do. It will > create a new string which will be assigned to a, at which point a and b won't > be pointing at the same object anymore. > > My mistake. Too many tests with and without the ! operator. I guess that my test used sub! instead of sub. This just proves my point that the whole thing is too confusing. >> I guess my problem is that the sub operations return a nil if they don't >> make a change rather than the unchanged object. > > They don't. sub! (the destructive method) will return nil, sub will return the > unchanged object. > > >> Maybe I need to write a version of sub or a method for >> it that returns the string regardless of whether it was changed or not. > > sub does that already. > >