From: Robert Klemme Date: 2005-10-18T21:56:58+09:00 Subject: Re: A couple of questions/statements from a Ruby neohacker Christophe Grandsire wrote: > Selon Robert Klemme : > >> >> I'm not so sure: the history of this list / ng has shown that this >> issue has irritated people over and over again. > > And it has also shown that many people agree with the convention as I > have stated it. And from the mouth of the master himself (from: > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/2331): > > "Unlike Scheme, not all change-in-place functions are named with > exclamation mark. In Ruby, exclamation mark means `MORE dangerous', > or `may cause surprise' than non-bang counterpart, i.g. in-place > modification by `gsub!' may let you surprise, comparing copying > `gsub'." > > As long as matz himself keeps with this convention (and he has shown > no sign of wanting to change it, especially since it would cover Ruby > with !'s - check the rest of the that thread -), I don't think we > should either. ! is *not* for destructive methods. It's for methods > that already exist in a non-bang form, and which do something similar > to them but in a dangerous way. There are plenty of destructive > methods that don't have a bang (because they don't have a > non-destructive version), and I don't believe it would be a gain to > add a ! to all of them, especially since it would in my opinion break > the aesthetics of Ruby. Certainly. And compatibility would suffer. Agreed. > So the convention that was >> followed in implementing this might be a different one from the >> publicly observed one. One reason for this might be that the term >> "dangerous" in this context is much broader and less precise than >> the term "destructive" (changing the receiver's state). Also >> "dangerous" is very context sensitive (even IO#puts might be >> regarded as dangerous in certain situations) which adds to the >> difficulty. > > But in this case it's simple: to get a !, a method has to have a > counterpart that does about the same work, and be more dangerous > (destructive, unsafe, etc...) than the other one. I never looked at it that way. Thanks for that hint! > It might be an interesting >> philosophical debate which of the two conventions is more adhered >> to, as a convention is a social thing... >> > > I believe the convention that bang methods always have a non-bang > alternative, and that destructive methods without a non-destructive > alternative don't have one, is pretty well followed. Anomalies I can think of off the top of my head: - Hash#update and Hash#merge: those do the same but #merge creates a new instance while #update doesn't. - Even for Array#delete it would make sense to have two variants - one that returns a copy with selected elements removed and another one that modifies in place. > I made a cursory look at about 4 years of the history of the > Ruby-talk list, and everything I read seems to indicate that the > convention is pretty well followed. In any case, I don't believe any > other convention would be better. Just remember that if you think of > putting a ! on your method you need one without the bang. If it > doesn't make sense to have such an alternative, your method doesn't > need a bang. Makes sense. Cheers robert