Hi --
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.