From: Sebastian Hungerecker Date: 2007-07-21T04:16:44+09:00 Subject: Re: Is there a replacement for sub? 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. > 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. -- NP: Milhaven - Clean Room Ist so, weil ist so Bleibt so, weil war so