Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention: Why Array#delete has no exclamation mark at the end?

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?

like image 967
Boris B. Avatar asked Feb 28 '12 20:02

Boris B.


2 Answers

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.

like image 53
sgtFloyd Avatar answered Oct 12 '22 00:10

sgtFloyd


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.

like image 41
Mark Thomas Avatar answered Oct 11 '22 23:10

Mark Thomas