From: Matt Armstrong Date: 2002-01-06T14:26:24+09:00 Subject: [ruby-talk:30332] Re: Ruby article on Freshmeat HarryO writes: > On Sun, 06 Jan 2002 10:21:32 +1100, Mark Hahn wrote: > > >> Imagine that you want to save text back to a file, but only if it >> changed: >> >> if str.upcase! then File.open('x.txt','w'){|f| f.write str} end > > I can certainly see that's a use for it ... but what's the > likelihood someone would really want to attempt to change something > to uppercase and only write it to a file if it changed :-). > > Don't worry, I realise you were just giving an example of how the > feature could be used. I just don't think it's likely to happen in > practice, so it doesn't help me understand why the '!' methods are > designed that way. > > I would say that the number of times that happens would be so few, > compared to the surprise factor having it return nil has, that would > not be sufficient justification for Matz to have made that design > decision. > > Sounds like Matz could write another book, similar to Bjarne > Stroustrup's "The Evolution of C++" :-). I think the official answer is to not use the ! version of the methods unless you really know what you're doing. Any given String instance may be referenced in many different places in a program. If you change it with ! as a matter of habit, can run into pretty interesting bugs. And usually the performance gain of ! is not necessary. -- matt