From: Peter Hickman Date: 2006-03-28T23:22:31+09:00 Subject: Re: Why do some methods names which replace the content in place doesn't have the "!" sign? Michael Ulm wrote: > Peter Hickman wrote: > >> Robert Dober wrote: >> >>> Please define "dangerous", I do not have the most remote idea what >>> might be >>> dangerous, apart of modifying state, which is "modifying instance >>> variable". >>> >> >> Dangerous is any function that modifies the caller. Thus >> fred.gsub(/[aeiou]/, '*') does not modify fred, it returns a new >> object that is the result of gsub(/[aeiou]/, '*') being applied to >> fred. However fred.gsub!(/[aeiou]/, '*') both returns a new object >> that is the result of gsub(/[aeiou]/, '*') being applied to fred >> *AND* modifies fred. > > You are mistaken. Consider > > a = [1, 2, 3] > a.delete(3) > a # => [1, 2] > I did not say that all methods that changed their caller had a !. I was only explaining what the dangerous meant in reference to those classes that had both a ! and non ! version of the method. Ruby is a little inconsistent but the ! is a syntactic - not a rule, not enforced in any way what so ever - convention (along with the ?) to make things clear where they might be ambiguous. There are plenty of methods that alter the caller but for gsub there are two versions of the same method and the ! is there to point out which one is dangerous. > It would if you knew C++. This is actually one of the more useful > features > of C++. If a method is declared 'const', the compiler ensures that it > does > not modify the instance variables of the object (unless you use really > deep > magic). This can be quite useful when debugging. > I program in C++ (and Java and Perl) but fail to see how a feature from a statically typed language would provide any benefit to a dynamic language such as Ruby. Without even trying to work out how you would hope to graft it on. I have never wanted for such a feature in Ruby but I have wanted features from Ruby in other languages. > I hope the condescending tone here was an accident. You too don't know > everything (see above), and you too make mistakes (see above). > I was cutting to the chase. I have read too many posts from people who have only read a couple of articles on Ruby and think that they can fix by making it more like C++, C, Java, Python, Perl, Visual Basic or whatever they think they understand. Inexperienced programmers seem to have a love of languages with many arcane and complex rules and when they don't find them they get all flustered and try and fix things rather than try and understand how we could possibly develop anything without them.