I'm learning Ruby and I've seen that an exclamation mark at the end of a method name by convention means that the method modifies self in some way. Why doesn't then Array#delete end with an exclamation mark, like slice! does, since delete deletes an element from self? Am I missing something fundamental?
10
votes
2 Answers
15
votes
To quote Matz (Ruby's chief engineer):
The bang (!) does not mean "destructive" nor lack of it mean non destructive either. The bang sign means "the bang version is more dangerous than its non bang counterpart; handle with care".
Since Array#delete doesn't have a less-dangerous counterpart, there is no need for the exclamation.
4
votes
The "bang" methods don't mean that it modifies the receiver. It is an indication of a method that is a more dangerous version of an existing method. See David A. Black's description of the difference, and the response to a request to change Ruby 2.0.
This is a very common misconception. Note the very highly-voted wrong answer here.
#deletecan do only delete so there is no need to flag it. Something like#upper_casecan either be assigned to a newly created object or overwrite the original object at the determination of the use. - ScottJShea