From: "Michael W. Ryder" <_mwryder@...> Date: 2007-07-22T11:40:00+09:00 Subject: Re: Is there a replacement for sub? dblack@wobblini.net wrote: > 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. > > Unfortunately, my background has always been that a function never returns nil if nothing is done. C has the ++ operator which can be very dangerous but it never returns nil. It may be safer to use a = a + 1 or even better b = a + 1, but I don't have to worry that a++ will return 0 unless a was -1 before. I realize that the ! operator is a shortcut, but if it is going to be a shortcut that is all it should be. They current way it is used with sub and gsub is that sometimes it returns the results in the specified object and other times it wipes out the object. This makes it Very hard to write a program where you are not always sure of the data, which in my case is a lot of the time. For example if I was cleaning a data file from a customer where he used dashes in a Social Security Number and I only wanted to remove up to the first two dashes in the number, knowing that if more are present that the number is invalid, I would use a variant of sub. I would not want to have to program for cases where there were no or one dash in the number. True, I could spend a lot of time trying to catch every abnormality, but then where is the advantage in using a language like Ruby, when I could do less work with a "primitive" language that does what I want without having to check what it is doing. I am not saying that Ruby is a "bad" language, as it has a lot of very nice features. My problem is that I am not always expecting some of the idiosyncrasies of the language. When I see something like sub! I expect that it will return my string with the substitution made or the unchanged string, not a nil. > David >