From: dblack@... Date: 2007-07-21T05:21:35+09:00 Subject: Re: Is there a replacement for sub? Hi -- On Sat, 21 Jul 2007, Michael W. Ryder wrote: > 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. Keep in mind that the ! means that the method is the "dangerous" version. In this case, the danger consists of both the changing of the object in place, and the returning of nil when there's no change. Any time you see a ! in a method name you've been warned that there's probably something you need to be particular aware of and careful about. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)