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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With